shell脚本中在if语句中比较空串而出现的问题line 5: [: =: unary operator expected
来源:互联网 发布:python函数传入文件名 编辑:程序博客网 时间:2024/04/27 17:29
一个shell脚本:
#!/bin/sh
#ifelif
echo "Enter your name: "
read NAME
if [ $NAME = "" ] || [ -z $NAME ]
then
echo "You don't enter your name."
elif [ $NAME = "root" ]
then
echo "Hello root."
elif [ $NAME = "Tom" ]
then
echo "Hello Tom."
else
echo "You aren't root or Tom in $NAME."
fi
在我测试空串的时候,出现问题,报错为./ifelif: line 5: [: =: unary operator expected,后来发现问题是出现在双引号上。
if [ $NAME = "" ] 在进行空串处理时,实际为if [ = "" ] ,这在处理时当然会报操作符异常,因此我们需要在变量上加上双引号即if [ "$NAME" = "" ],这样你在测试空串时,为if [ "" = "" ],shell脚本才会通过。 虽然shell中有没有双引号很多情况下是一致的,但是为了保证正确性,在比较字符串时加上还是一种好的习惯。
0 0
- shell脚本中在if语句中比较空串而出现的问题line 5: [: =: unary operator expected
- shell脚本报错"[: =: unary operator expected"的解决方案
- 【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"
- 3. laravel 包含头尾文件
- loadrunner服务端压力测试工具 破解方法
- Java设计模式透析之 —— 模板方法(Template Method)
- scanf和gets读取字符串
- openfire集群失败原因(关掉virbr0)
- shell脚本中在if语句中比较空串而出现的问题line 5: [: =: unary operator expected
- springmvc MultipartFile转file和图片压缩
- Digester解析xml文件
- ioS-------------APP使用自定义字体包
- unauthorizedAccessException
- java注解
- Linux系统下通过Socket文件描述符寻找连接状态
- ViewDragHelper详解(一)- 可拖动的view
- FREAK和ORB特征描述子