iftest -z"$abc";thenecho"It's an empty string."fi 这段代码输出的结果仍然是: It's an empty string. 判断字符串相等 test 支持两个运算符来判断字符串相等:= 和 ==。 abc="hello"iftest"hello"=="$abc";thenecho"You got same string."fi 运行上面代码的输出结果为: You got samestring. 因...
#!/bin/bash function empty_string() {iftest -n $1; then echo'(1) -n $1 :' "No quote: not empty."fiif[ -z $1]; then echo'(2) -z $1 :' "No quote: empty."fiiftest -n "$1"; then echo'(3) -n "$1":' "Quote : not empty."fiif[ -z "$1"]; then echo'(4) ...
bash will complain about that. Also, since any bash test without an argument will always return true, it is advisable to quote the variables so that the test will have at least an (empty) argument, irrespective of whether the variable is set or not. ...
-z STRING True if string is empty. -n STRING STRING True if string is not empty. 即,test命令使用-z STRING操作符来判断STRING字符串的长度是否为 0。 如果为 0,就是空字符串,会返回 true。 具体写法是test -z STRING,使用[命令则写为[ -z STRING ]。 -n STRING操作符判断STRING字符串的长度是否...
Bash技巧:对比 test判断字符串是否为空的用法,#!/bin/bashfunctionempty_string(){iftest-n$1;thenecho'(1)-n$1:'"Noquote:notempty."fiif[-z$1];thenecho'(2)-z$1:'"Noquote
test operand1 operator operand2 对某些测试来说,只需要一个操作数(operand2)通常是下面这种情况的简写: [ operand1 operator operand2 ] 为了让我们的讨论更接地气一点,给出下面一些例子: #!/bin/bash X=3 Y=4 empty_string="" if [ $X -lt $Y ]# is $X less than $Y ?
if [[ -z $string ]]; then echo "The string is empty." else echo "The string is not empty." fi 这是我们执行脚本时的结果: 代码语言:txt AI代码解释 $ ./test.sh Strings are different. 例4 -n运算符还可用于测试字符串长度是否不为零。
iftest"abc"="abc" then echo"Strings are equal" fi #检测字符串是否非空 iftest-n"$var" then echo"String is not empty" fi 4. test支持逻辑运算符。 •! EXPRESSION:逻辑非 •-a:逻辑与 •-o:逻辑或 下面是一些示例用法: #判断两个条件是否同时满足 iftest$var1-eq10-a$var2-lt20 then...
string="hello" if [[ -n $string ]]; then echo "The string is not empty." else echo "The string is empty." fi 这是我们执行脚本时的结果: $ ./test.sh Strings are different. 例5 我们还可以使用小于<和大于>运算符来检查一个字符串是否比另一个字符串多。
string "0" is false } else { echo "string \"0\" is not false \r\n"; } 空数组...false \r\n"; } else { echo "string \"0.0\" is not false \r\n"; // 输出:string "0.0" is not false } 正确地检查一个变量是否为空应该使用...php if (empty($var)) { ... } 原文链接:...