shell脚本报错:"[: =: unary operator expected"
2014-05-31 22:18
288 查看
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://kitsnail.blog.51cto.com/4052186/1420529
=: 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://kitsnail.blog.51cto.com/4052186/1420529
相关文章推荐
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell 脚本报错"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- linux编程--shell脚本报错:"[: =: unary operator expected"
- shell脚本报错"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"
- shell脚本报错:"[: =: unary operator expected"