String1 and String2 are equal. String1 and String3 are not equal. 这里,如果我们先用 = 运算符比较 String1 和 String2。由于 String1 和 String2 都具有相同的长度,具有相同的字符序列,比较运算符返回 true,因此我们得到 String1 and String2 are equal.. 作为程序中第一个 if-else 块的输出。 同样...
string1="apples" string2="oranges" if [ "$string1" = "$string2" ]; then echo "The two strings are equal." else echo "The two strings are not equal." fi 这是我们执行脚本时的结果: 代码语言:txt AI代码解释 $ ./test.sh The two strings are not equal. 例2 我们还可以使用运算符来...
string2="oranges" if [ "$string1" = "$string2" ]; then echo "The two strings are equal." else echo "The two strings are not equal." fi 这是我们执行脚本时的结果: $ ./test.sh The two strings are not equal. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。 #!/bin/bash...
else echo "Strings are not equal." fi 2. 使用 tr 命令去除所有空白字符 代码语言:txt 复制 string1=$(echo "your_string" | tr -d '[:space:]') string2=$(echo "your_string" | tr -d '[:space:]') if [ "$string1" == "$string2" ]; then echo "Strings are equal." else echo ...
$ ./test.sh The two strings are not equal. 1. 2. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。 AI检测代码解析 #!/bin/bash string1="apples" string2="oranges" if [ "$string1" != "$string2" ]; then echo "Strings are different." ...
/bin/bashstring1="apples"string2="oranges"if["$string1"="$string2"];thenecho"Thetwostringsareequal."elseecho"Thetwostringsarenotequal."fi 这是我们执行脚本时的结果: $./test.shThetwostringsarenotequal. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。
test EXPRESSION [ EXPRESSION ] [[ EXPRESSION ]] 前两种是等价的,应该是没有区别的。注意EXPRESSION两边与中括号之间需要有空格。 双中括号与前两者的区别,主要在于表达式中的操作符如果是这种情况的时候。 [[ string1 >string2 ]] [[ string1< string2 ]] ...
-ge n1 大于等于 n2 (greater than or equal) -le n1 小于等于 n2 (less than or equal) 5. 判定字符串的数据 test -z string 判定字符串是否为 0 ?若 string 为空字符串,则为 true test -n string 判定字符串是否非为 0 ?若 string 为空字符串,则为 false。
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...
string1 > string2如果对string1和string2按字母顺序进行排序,string1排在string2后面 图表3: Bash 字符串逻辑操作符 首先,检查字符串长度。比较表达式中$MyVar两边的双引号不能省略(你仍应该在目录~/testdir下 )。 [student@studentvm1 testdir]$ MyVar="" ; if [ -z "" ] ; then echo "MyVar is zer...