我试图运行以下shell脚本,它应该检查一个字符串既不是空间也不是空。然而,我得到相同的输出所有3个提到的字符串。我试着使用“[[”语法,但也没有效果。
这里是我的代码:
str="Hello World" str2=" " str3="" if [ ! -z "$str" -a "$str"!=" " ]; then echo "Str is not null or space" fi if [ ! -z "$str2" -a "$str2"!=" " ]; then echo "Str2 is not null or space" fi if [ ! -z "$str3" -a "$str3"!=" " ]; then echo "Str3 is not null or space" fi
我得到以下输出:
# ./checkCond.sh Str is not null or space Str2 is not null or space
您在!=的任一侧都需要一个空格。将您的代码更改为:
str="Hello World" str2=" " str3="" if [ ! -z "$str" -a "$str" != " " ]; then echo "Str is not null or space" fi if [ ! -z "$str2" -a "$str2" != " " ]; then echo "Str2 is not null or space" fi if [ ! -z "$str3" -a "$str3" != " " ]; then echo "Str3 is not null or space" fi