加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 检查字符串不是空的,不是shell脚本中的空格

发布时间:2020-12-15 16:13:43 所属栏目:安全 来源:网络整理
导读:我试图运行以下shell脚本,它应该检查一个字符串既不是空间也不是空。然而,我得到相同的输出所有3个提到的字符串。我试着使用“[[”语法,但也没有效果。 这里是我的代码: str="Hello World"str2=" "str3=""if [ ! -z "$str" -a "$str"!=" " ]; then echo
我试图运行以下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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读