bash – 检查shell脚本中的字符串是否既不为空也不是空格
发布时间:2020-12-15 19:04:14 所属栏目:安全 来源:网络整理
导读:我试图运行以下shell脚本,它应该检查字符串既不是空格也不是空的.但是,我得到的所有3个字符串的输出都相同.我尝试过使用“[[”语法,但无济于事. 这是我的代码: str="Hello World"str2=" "str3=""if [ ! -z "$str" -a "$str"!=" " ]; then echo "Str is not
我试图运行以下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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |