bash – 检查shell脚本中的字符串是否既不为空也不是空格

前端之家收集整理的这篇文章主要介绍了bash – 检查shell脚本中的字符串是否既不为空也不是空格前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图运行以下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
原文链接:https://www.f2er.com/bash/387221.html

猜你在找的Bash相关文章