我试图运行以下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