如果条件,Bash 中的“一元运算符预期”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13617843/
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-09 23:01:17  来源:igfitidea点击:

"unary operator expected" error in Bash if condition

bashshell

提问by Jordanss10

I've been trying to figure out whats wrong with this but just can't figure it out..

我一直在试图弄清楚这有什么问题,但就是无法弄清楚..

This is the part seems to be getting an error..

这是部分似乎出现错误..

elif [ $operation = "man" ]; then
    if [ $aug1 = "add" ]; then         # <- Line 75
    echo "Man Page for: add"
    echo ""
    echo "Syntax: add [number 1] [number 2]"
    echo ""
    echo "Description:"
    echo "Add two different numbers together."
    echo ""
    echo "Info:"
    echo "Added in v1.0"
    echo ""
elif [ -z $aug1 ]; then
    echo "Please specify a command to read the man page."
else
    echo "There is no manual page for that command."
fi

I get this error:

我收到此错误:

calc_1.2: line 75: [: =: unary operator expected

回答by rici

If you know you're always going to use bash, it's much easier to always use the double bracket conditional compound command [[ ... ]], instead of the Posix-compatible single bracket version [ ... ]. Inside a [[ ... ]]compound, word-splitting and pathname expansion are not applied to words, so you can rely on

如果你知道你总是要使用 bash,那么总是使用双括号条件复合命令要容易得多[[ ... ]],而不是与 Posix 兼容的单括号版本[ ... ]。在[[ ... ]]复合词中,分词和路径名扩展不适用于词,因此您可以依靠

if [[ $aug1 == "and" ]];

to compare the value of $aug1with the string and.

将 的值$aug1与字符串进行比较and

If you use [ ... ], you always need to remember to double quote variables like this:

如果你使用[ ... ],你总是需要记住像这样双引号变量:

if [ "$aug1" = "and" ];

If you don't quote the variable expansion and the variable is undefined or empty, it vanishes from the scene of the crime, leaving only

如果不引用变量扩展并且变量未定义或为空,它就会从犯罪现场消失,只留下

if [ = "and" ]; 

which is not a valid syntax. (It would also fail with a different error message if $aug1included white space or shell metacharacters.)

这不是有效的语法。(如果$aug1包含空格或 shell 元字符,它也会失败并显示不同的错误消息。)

The modern [[operator has lots of other nice features, including regular expression matching.

现代[[运算符还有许多其他不错的功能,包括正则表达式匹配。

回答by Juha Vehnia

Took me a while to find this but note that if you have a spacing error you will also get the same error:

我花了一段时间才找到这个,但请注意,如果您有间距错误,您也会收到相同的错误:

[: =: unary operator expected

Correct:

正确的:

if [ "$APP_ENV" = "staging" ]

vs

对比

if ["$APP_ENV" = "staging" ]

As always setting -xdebug variable helps to find these:

一如既往地设置-x调试变量有助于找到这些:

set -x

回答by yilee

Try assigning a value to $aug1before use it in if[]statements; the error message will disappear afterwards.

尝试$aug1if[]语句中使用之前为其赋值;之后错误信息将消失。

回答by Renan Moura

u say ... :

你说 ... :

elif [ $operation = "man" ]; then if [ $aug1 = "add" ]; then # <- Line 75 echo "Man Page for: add" echo "" echo "Syntax: add [number 1] [number 2]" echo "" echo "Description:" echo "Add two different numbers together." echo "" echo "Info:" echo "Added in v1.0" echo "" elif [ -z $aug1 ]; then echo "Please specify a command to read the man page." else echo "There is no manual page for that command." fi

elif [ $operation = "man" ]; 然后如果 [ $aug1 = "add" ]; then # <- Line 75 echo "Man Page for: add" echo "" echo "Syntax: add [number 1] [number 2]" echo "" echo "Description:" echo "将两个不同的数字相加。" echo "" echo "Info:" echo "Added in v1.0" echo "" elif [ -z $aug1 ]; 然后回显“请指定一个命令来阅读手册页。” else echo "没有该命令的手册页。" 菲

i think its just the space... do like this:

我认为它只是空间......这样做:

elif [$operation = "man"]; then
    if [ $aug1 = "add" ]; then         # <- Line 75
    echo "Man Page for: add"
    echo ""
    echo "Syntax: add [number 1] [number 2]"
    echo ""
    echo "Description:"
    echo "Add two different numbers together."
    echo ""
    echo "Info:"
    echo "Added in v1.0"
    echo ""
elif [-z $aug1]; then
    echo "Please specify a command to read the man page."
else
    echo "There is no manual page for that command."
fi

回答by el guesto

You can also set a default value for the variable, so you don't need to use two "[", which amounts to two processes ("[" is actually a program) instead of one.

您还可以为变量设置一个默认值,这样您就不需要使用两个“[”,这相当于两个进程(“[”实际上是一个程序)而不是一个。

It goes by this syntax: ${VARIABLE:-default}.

它遵循以下语法:${VARIABLE:-default}。

The whole thing has to be thought in such a way that this "default" value is something distinct from a "valid" value/content.

整个事情必须以这种“默认”值与“有效”值/内容不同的方式来考虑。

If that's not possible for some reason you probably need to add a step like checking if there's a value at all, along the lines of "if [ -z $VARIABLE ] ; then echo "the variable needs to be filled"", or "if [ ! -z $VARIABLE ] ; then #everything is fine, proceed with the rest of the script".

如果由于某种原因这是不可能的,您可能需要添加一个步骤,例如检查是否有值,沿着“if [ -z $VARIABLE ] ; then echo“需要填充变量””或“ if [ ! -z $VARIABLE ] ; 那么#一切都很好,继续执行脚本的其余部分”。