/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 "$1":' "Quote : empty."f...
-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字符串的长度是否...
if test -z "$abc"; then echo "It's an empty string." fi 这段代码输出的结果仍然是: It's an empty string. 判断字符串相等 test 支持两个运算符来判断字符串相等:= 和 ==。 abc="hello" if test "hello" == "$abc"; then echo "You got same string." fi 运行上面代码的输出结果为: Y...
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. 因...
test 命令的主要用途是检查字符串、比较整数和检查文件的属性。本文将通过简单的示例介绍 test 命令的常见用法。 检查字符串 判断字符串是否为空 test 检查字符串时,非空的字符串返回 true,空字符串或者没有参数都返回 false。 iftest"$abc";thenecho"It's not an empty string."elseecho"It's an empty stri...
单左括号“[ " 命令,这是一个可执行程序,位于 /bin 或者 /usr/bin 下面,它的使用方法和 test ...
Bash技巧:对比 test判断字符串是否为空的用法,#!/bin/bashfunctionempty_string(){iftest-n$1;thenecho'(1)-n$1:'"Noquote:notempty."fiif[-z$1];thenecho'(2)-z$1:'"Noquote
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运算符还可用于测试字符串长度是否不为零。
String="Test"if[[-n$String]];thenecho"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.作为给定程序的输出...
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 我们还可以使用小于<和大于>运算符来检查一个字符串是否比另一个字符串多。