微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.jb51.cc/bash/387221.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐