-z is the second supported bash string comparison operator used to check if a string is empty or not. The -z operator functions similarly like -n operator. Below is an example: Most importantly, you should add spaces around the square brackets. If there are no spaces, bash will complain ...
-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字符串的长度是否...
You got samestring. 因为= 看起来更像是在赋值,所以我更倾向于使用 ==。但是当你的脚本需要跨平台时,最好还是使用 =(test 中的 = 是 POSIX 标准定义的!)。 如果要判断两个字符串不相等,可以使用 != 运算符: abc="helloo"iftest"hello"!="$abc";thenecho"Your word is not 'hello'."fi 运行上面...
echo "$a : The string is not empty" else echo "$a : The string is empty" fi 结果 abc = efg: a != b -n abc : The string length is not 0 abc : The string is not empty 文件测试运算符 实例: #!/bin/bash file="/home/shiyanlou/test.sh" if [ -r $file ] then echo "The ...
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运算符还可用于测试字符串长度是否不为零。
if [[ -z $string ]]; then echo "The string is empty." else echo "The string is not empty." fi 这是我们执行脚本时的结果: $ ./test.sh Strings are different. 例4 -n运算符还可用于测试字符串长度是否不为零。 #!/bin/bash string="hello" ...
$ ./test.sh Strings are different. 1. 2. 例3 我们可以与字符串一起使用的另一个运算符是-z,它允许我们测试字符串长度是否为 0。 #!/bin/bash string="" if [[ -z $string ]]; then echo "The string is empty." else echo "The string is not empty." ...
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)) { ... } 原文链接:...
{STRING1} ! = {STRING2} True if both lines are not equal. {STRING1} { {STRING2} True if {STRING1} is sorted to lexicographic {STRING2} {STRING1} } {STRING2} True {string1} sorts after lexically{STRING2} Any built-in test at first glance may seem simple, but this is not alwa...
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...