Here is another option, “-n”, to check whether the specified string is empty or not. It works on the rule of checking the length of a string by counting the string characters in it. If the length of a particular string turns out to be other than zero, it will return “true”; o...
str="" 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 lo...
echo "The string is not empty." fi 这是我们执行脚本时的结果: 代码语言:txt AI代码解释 $ ./test.sh Strings are different. 例4 -n运算符还可用于测试字符串长度是否不为零。 代码语言:txt AI代码解释 #!/bin/bash string="hello" if [[ -n $string ]]; then echo "The string is not empty...
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...
Afterdeclaring a string variable, use the-zoperator in anif statementto check whether a string is empty or not: 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!
-n STRING STRING True if string is not empty. 即,test命令使用-z STRING操作符来判断STRING字符串的长度是否为 0。 如果为 0,就是空字符串,会返回 true。 具体写法是test -z STRING,使用[命令则写为[ -z STRING ]。 -n STRING操作符判断STRING字符串的长度是否为 0。
if [[ -z $String ]]; then echo "The variable String is an empty string." fi 输出: The variable String is an empty string. 在这个程序中,string 是一个空变量。由于 -z 运算符返回 true,如果 string 的长度是 0,因此我们得到 The variable String is an empty string. 作为给定程序的输出。
echo "INT is not an integer." >&2 exit 1 fi只要是算术表达式,都能用于((...))语法,详见《Bash 的算术运算》一章。普通命令的逻辑运算如果if结构使用的不是test命令,而是普通命令,比如上一节的((...))算术运算,或者test命令与普通命令混用,那么可以使用 Bash 的命令控制操作符&&(AND)和||(OR),进行...
if [[ -z "$string" ]]; then echo "String is empty" elif [[ -n "$string" ]]; then echo "String is not empty" fi 字符串引号 str="czx" echo "Hello ${str}" # 显示 Hello czx echo 'Hello ${str}' # 显示 Hello ${str} 函数 get_name() { echo "czx" } echo "My nam...
is readable-s FILE_NAM # TrueifFILE_NAM existsandisnotempty-w FILE_NAM # TrueifFILE_NAM has write permission-x FILE_NAM # TrueifFILE_NAM is executable#字符串测试操作-z STRING # TrueifSTRING is empty-n STRING # TrueifSTRING isnotemptySTRING1...