shell脚本报错:"[: =: unary operator expected"
来源:互联网 发布:sql怎么设置 identity 编辑:程序博客网 时间:2024/06/14 01:36
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中有没有双引号在很多情况下是一致的。
阅读全文
0 0
- 【shell】 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"
- 大获成功!SpaceX第二次发射并回收重复利用火箭
- 极智嘉(Geek+)CEO郑勇:智能物流和仓储改变商业未来 | CCF-GAIR 2017
- GlobalFoundries公布7nm工艺细节:最大芯片尺寸700mm²,2018年量产
- python里使用正则表达式的选项参数
- 事件基础
- shell脚本报错:"[: =: unary operator expected"
- 自我介绍
- java小项目之用户信息添加
- 将大脑比作计算机,正在阻碍脑科学研究
- IntelliJ IDEA Kotlin第一步从hello world开始
- RadioButton+RadioGroup
- 数据结构学习记录-链式栈
- 深度好贴,mark一下!
- MyBatis 全局配置文件解析