bash 检查shell脚本中的字符串是否既非空也非空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13509508/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if string is neither empty nor space in shell script
提问by Shubhanshu Mishra
I am trying to run the following shell script which is supposed to check if a string is neither space nor empty. However, I am getting the same output for all the 3 mentioned strings. I have tried using the "[[" syntax as well but to no avail.
我正在尝试运行以下 shell 脚本,它应该检查字符串是否既不是空格也不是空的。但是,对于所有提到的 3 个字符串,我都得到了相同的输出。我也尝试过使用“[[”语法,但无济于事。
Here is my code:
这是我的代码:
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
I am getting the following output:
我得到以下输出:
# ./checkCond.sh
Str is not null or space
Str2 is not null or space
回答by dogbane
You need a space on either side of the !=
. Change your code to:
您需要在!=
. 将您的代码更改为:
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
回答by RajeshKashyap
For checking the empty string in shell
用于检查 shell 中的空字符串
if [ "$str" == "" ];then
echo NULL
fi
OR
或者
if [ ! "$str" ];then
echo NULL
fi
回答by hyde
In case you need to check against any amount of whitespace, not just single space, you can do this:
如果您需要检查任意数量的空格,而不仅仅是单个空格,您可以这样做:
To strip string of extra white space (also condences whitespace in the middle to one space):
去除一串额外的空格(也将中间的空格压缩为一个空格):
trimmed=`echo -- $original`
The --
ensures that if $original
contains switches understood by echo, they'll still be considered as normal arguments to be echoed. Also it's important to not put ""
around $original
, or the spaces will not get removed.
在--
确保如果$original
包含交换机通过回声的理解,他们仍然被视为普通参数进行呼应。同样重要的是不要放在""
周围$original
,否则空格不会被删除。
After that you can just check if $trimmed
is empty.
之后,您可以检查是否$trimmed
为空。
[ -z "$trimmed" ] && echo "empty!"
回答by elomage
Another quick test for a string to have something in it but space.
另一个快速测试字符串中是否有除空格之外的内容。
if [[ -n "${str// /}" ]]; then
echo "It is not empty!"
fi
"-n" means non-zero length string.
“-n”表示非零长度字符串。
Then the first two slashes mean match allof the following, in our case space(s). Then the third slash is followed with the replacement (empty) string and closed with "}". Note the difference from the usual regular expression syntax.
然后前两个斜杠表示匹配以下所有内容,在我们的例子中是空格。然后第三个斜杠后跟替换(空)字符串并以“}”结束。请注意与通常的正则表达式语法的区别。
You can read more about string manipulation in bash shell scripting here.
您可以在此处阅读有关bash shell 脚本中的字符串操作的更多信息。
回答by Eugene Yarmash
To check if a string is empty or contains only whitespace you could use:
要检查字符串是否为空或仅包含空格,您可以使用:
shopt -s extglob # more powerful pattern matching
if [ -n "${str##+([[:space:]])}" ]; then
echo '$str is not null or space'
fi
See Shell Parameter Expansionand Pattern Matchingin the Bash Manual.
请参阅Bash 手册中的Shell 参数扩展和模式匹配。
回答by Harsh Shandilya
[ $(echo $variable_to_test | sed s/\n// | sed s/\ //) == "" ] && echo "String is empty"
Stripping all newlines and spaces from the string will cause a blank one to be reduced to nothing which can be tested and acted upon
从字符串中删除所有换行符和空格将导致空白的减少到可以测试和采取行动的空白