/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) -z...
-z STRING Trueifstring is empty. -n STRING STRING Trueifstring is not empty. 即,test命令使用-z STRING操作符来判断STRING字符串的长度是否为 0。 如果为 0,就是空字符串,会返回 true。 具体写法是test -z STRING,使用[命令则写为[ -z STRING ]。 -n STRING操作符判断STRING字符串的长度是否为 0。
Bash技巧:对比 test判断字符串是否为空的用法,#!/bin/bashfunctionempty_string(){iftest-n$1;thenecho'(1)-n$1:'"Noquote:notempty."fiif[-z$1];thenecho'(2)-z$1:'"Noquote
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. 因...
在bash里,[] 是 test 命令的 alias,这个命令在参数只是一个变量时,会把变量当字符串,所以[ true...
test 命令的主要用途是检查字符串、比较整数和检查文件的属性。本文将通过简单的示例介绍 test 命令的常见用法。 检查字符串 判断字符串是否为空 test 检查字符串时,非空的字符串返回 true,空字符串或者没有参数都返回 false。 iftest"$abc";thenecho"It's not an empty string."elseecho"It's an empty stri...
When we have executed our code with Bash instruction after saving the code, we have the result as we expected, i.e., “String val is empty”. Example 04 You can also use the “test” method to check for the string emptiness, as shown below. Within this method, you need to test the...
$JAIL set to a non-empty string (1)# $JAIL set to the empty string (2)# $JAIL can be unset (3)###echo "*** Current value of \$JAIL is '$JAIL' ($HINT) ***" ## Determine if a bash variable is empty or not ##if [ -z "${JAIL}" ]; then echo "JAIL is unset ...
执行man test可以查看所有测试表达式可以比较和判断的类型。 直接执行以下脚本: #!/bin/sh if [ "$SHELL" = "/bin/bash" ]; then echo "your login shell is the bash (bourne again shell)" else echo "your login shell is not bash but $SHELL" ...
if [[ -n $string ]]; then echo "The string is not empty." else echo "The string is empty." fi 这是我们执行脚本时的结果: 代码语言:txt 复制 $ ./test.sh Strings are different. 例5 我们还可以使用小于<和大于>运算符来检查一个字符串是否比另一个字符串多。