shell脚本报错:"[: =: unary operator expected"

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本报错:"[: =: unary operator expected"相关的知识,希望对你有一定的参考价值。

 shell脚本报错:"[: =: unary operator expected"

    在匹配字符串相等时,我用了类似这样的语句:

if [ $STATUS == "OK" ]; then     

echo "OK"

fi

    在运行时出现了 [: =: unary operator expected 的错误,就一直找不到原因,尝试了删除等号两侧的空格和括号里的空格都不管用,最后baidu了一下,才找到原因。把语句改成这样就不会出错了.

if [[ $STATUS = "OK" ]]; 

then     

echo "OK"

fi

    究其原因,是因为如果变量STATUS值为空,那么就成了 [ = "OK"] ,显然 [ 和 "OK" 不相等并且缺少了 [ 符号,所以报了这样的错误。当然不总是出错,如果变量STATUS值不为空,程序就正常了,所以这样的错误还是很隐蔽的。

    或者用下面的方法也能避免这种错 误:if [ "$STATUS"x == "OK"x ]; then     echo

"OK"fi。当然,x也可以是其他字符。顺便提一点,shell中有没有双引号在很多情况下是一致的。

 

转自:http://hi.baidu.com/vishare/blog/item/bd8ab9ee289753252cf53417.html

以上是关于shell脚本报错:"[: =: unary operator expected"的主要内容,如果未能解决你的问题,请参考以下文章

shell脚本报错{"errcode":40035,"errmsg":"缺少参数 json"}

Linux Shell脚本报错:command not found

linux shell脚本 总报错sh11-2.sh: 9: Syntax error: "(" unexpected

jenkins执行groovy 脚本报错

shell脚本在win下被修改后报错语法错误Syntax error: end of file unexpected (expecting "then")

shell脚本启动 :字符串判空有空格报错:binary operator expected