bash [: -gt: 预期的一元运算符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24953465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 10:57:13  来源:igfitidea点击:

[: -gt: unary operator expected

bash

提问by James

I don't write a lot of Bash, so I'm a bit stumped as to how to fix this. I need to check whether a value returned from a command is greater than x. When it runs though I get [: -gt: unary operator expectedwhich I'm unable to fix.

我不写很多 Bash,所以我对如何解决这个问题有点困惑。我需要检查从命令返回的值是否大于x. 当它运行时,我得到[: -gt: unary operator expected了我无法修复的信息。

Here is my script,

这是我的脚本,

#!/bin/sh
ERROR=0
PHPCPDLevel=100

# PHPCPD
echo "PHP CopyPaste Detection (Limit is at least ${PHPCPDLevel}%"
PHPCPD="phpcpd ."
if [[ `echo $PHPCPD | grep "%" | cut -d'.' -f1` -gt "$PHPCPDLevel" ]]
  then
    echo $PHPCPD
    ERROR=1
  else
    echo "... -> Only `echo $PHPCPD | grep "%" | cut -d'.' -f1`%"
fi

echo "Finished!"
exit $ERROR

Update:I think I've done it:

更新:我想我已经做到了:

#!/bin/sh
ERROR=0
PHPCPDLevel=25

# PHPCPD
echo "PHP CopyPaste Detection (Limit is at most ${PHPCPDLevel}%)"
PHPCPD="phpcpd ."
PERCENTAGE=$($PHPCPD | grep "%" | cut -d'.' -f1)
if [ ${PERCENTAGE} -gt ${PHPCPDLevel} ]
  then
    echo $PHPCPD
    ERROR=1
  else
    echo "Only $PERCENTAGE%"
fi

exit $ERROR

回答by David W.

Remember that [is a command. It maybe built into your shell, but it's still a command. It is expecting a particular set of parameters, and will give you an error when it gets something it doesn't understand. In fact, you can replace [ ... ]with test ...if that makes things a bit easier to understand:

记住这[是一个命令。它可能内置在您的 shell 中,但它仍然是一个命令。它期待一组特定的参数,当它得到它不理解的东西时会给你一个错误。事实上,你可以[ ... ]test ...if替换,这会让事情更容易理解:

For example:

例如:

test -gt 34

Will return:

将返回:

bash: test: -gt: unary operator expected

Hmmm... same error message.

嗯...同样的错误信息。

When you get things like this, you should use set -xvand set +xvaround the problem area of your shell script. The set -xvwill print out the shell command to be executed, and then will show you what the command line looked like after it has been mangledI mean interpolatedby the shell.

当你得到这样的东西时,你应该使用set -xvset +xv绕过你的 shell 脚本的问题区域。该set -xv会打印出要执行的shell命令,然后会告诉你什么样的命令行看起来像在它已经错位我的意思是插值通过外壳。

I suspect that your error is:

我怀疑你的错误是:

if [ ${PERCENTAGE} -gt ${PHPCPDLevel} ]

That ${PERCENTAGE}is a blank value. If you use [[ ... ]]instead of [ ... ]you won't get that error. The [[ ... ]]is parsed a bit differently than [ ... ]because it's a compound command. The shell interpolations are done after the initial command is parsed, so it's a bit more forgiving if you miss a quotation mark or strings contain unexpected characters.

${PERCENTAGE}是一个空白值。如果你使用[[ ... ]]而不是[ ... ]你不会得到那个错误。的[[ ... ]]解析方式与[ ... ]因为它是复合命令而有所不同。shell 插值是在解析初始命令后完成的,因此如果您错过引号或字符串包含意外字符,则更宽容一些。

So:

所以:

ERROR=0
PHPCPDLevel=25

# PHPCPD
echo "PHP CopyPaste Detection (Limit is at most ${PHPCPDLevel}%)"
export PS4="$LINENO: "     # Prints out the line number being executed by debug
set -xv                     # Turn on debugging
PHPCPD="phpcpd ."
PERCENTAGE=$($PHPCPD | grep "%" | cut -d'.' -f1)
if [[ ${PERCENTAGE} -gt ${PHPCPDLevel} ]]  # Use [[ ... ]] instead of [ .. ]
then
    echo $PHPCPD
    ERROR=1
else
    echo "Only $PERCENTAGE%"
fi
set +xv                     # Turn off debugging

exit $ERROR

Now, you'll see what the various commands that set environment variables are returning, and possibly see something you didn't quite expect.

现在,您将看到设置环境变量的各种命令返回的内容,并且可能会看到您不太期望的内容。

回答by user000001

You can't use double brackets [[ ... ]]in sh. Change your sheebang to

不能[[ ... ]]sh. 改变你的sheebang

#!/bin/bash

or change the syntax to use single brackets [ ... ]. Don't forget to quote the terms inside the expression if you do that.

或更改语法以使用单括号[ ... ]。如果这样做,请不要忘记引用表达式中的术语。