-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 ...
具体写法是test -z STRING,使用[命令则写为[ -z STRING ]。 -n STRING操作符判断STRING字符串的长度是否为 0。 如果不为 0,就不是空字符串,会返回 true。 具体写法是test -n STRING,使用[命令则写为[ -n STRING ]。 可以省略-n操作符,直接写为test STRING、或者[ STRING ]。 注意:在实际使用时,要...
echo "-n $a : The string length is not 0" else echo "-n $a : The string length is 0" fi if [ $a ] then 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 strin...
if [ "$str" == "" ]; then echo "String is empty" else echo "String is not empty" fi 字符串长度比较:可以使用大于号(>)、小于号(<)来比较两个字符串的长度。例如: 代码语言:txt str1="Hello" str2="World" if [ ${#str1} -gt ${#str2} ]; then echo "str1 is longer than s...
MY_STRING="" if [ -z $MYSTRING ] then echo "String is empty" else echo "String is not empty" fi For more Shell scripting tips,check out or Bash/Shell scripting articles!
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运算符还可用于测试字符串长度是否不为零。
-s FILE_NAM # True if FILE_NAM exists and is not empty -w FILE_NAM # True if FILE_NAM has write permission -x FILE_NAM # True if FILE_NAM is executable 字符串测试操作 -z STRING # True if STRING is empty -n STRING # True if STRING is not empty ...
if [[ -n $String ]]; then echo "The variable String is not an empty string." fi 输出: The variable String is not an empty string. 在这个程序中,String 是一个非空变量。由于 -n 运算符返回 true,如果 string 的长度不是 0,因此我们得到 The variable String is not an empty string. 作为...
if[$a]thenecho"$a: The string is not empty"elseecho"$a: The string is empty"fi 文件测试运算符 浮点运算 expr 只能用于整数计算,可以使用 bc 或者 awk 进行浮点数运算。 #!/bin/bashradius=2.4 pi=3.14159 girth=$(echo"scale=4; 3.14 * 2 *$radius"| bc) ...
echo "$a : The string is not empty" else echo "$a : The string is empty" fi 1. 2. 3. 4. 5. 6. 7. 8. 文件比较运算符 如下例: #!/bin/bash file="/test.sh" # 由于没有给出条件,所以返回true if [ ! ] then echo "File exists" ...