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...
String1 and String2 are equal. String1 and String3 are not equal. 这里,如果我们先用 = 运算符比较 String1 和 String2。由于 String1 和 String2 都具有相同的长度,具有相同的字符序列,比较运算符返回 true,因此我们得到 String1 and String2 are equal.. 作为程序中第一个 if-else 块的输出。 同样...
str1="Hello" str2="World" if [ "$str1" != "$str2" ]; then echo "Strings are not equal" else echo "Strings are equal" fi 字符串为空比较:可以使用双等号(==)来比较一个字符串是否为空。例如: 代码语言:txt 复制 str="" if [ "$str" == "" ]; then echo "String is empty" els...
echo "Strings are equal." # 这将不会打印,因为str1包含换行符,而str2是'n' else echo "Strings are not equal." fi 但请注意,即使这样,上面的比较也不会输出 "Strings are equal.",因为$str1实际上包含一个换行符,而$str2只是一个字母n。在bash中直接比较包含特殊字符(如换行符)的字符串时,你需要...
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 我们还可以使用运算符来测试两个字符串是否不相等!=。
str1 - the first String, may be null str2 - the second String, may be null Returns: true if the Strings are equal, case insensitive, or both null In Bash 判断字符串相等 格式1:test "$S1" = "$S2" 格式2:[ "$S1" = "$S2" ] ...
-ne:是否不等于; 不相等,全拼为not equal -lt:是否小于; 小于,全拼为less than -le:是否小于等于; 小于等于,全拼为less equal 字符串表达式: ==:是否等于; >:是否大于; <:是否小于; !=:是否不等于; =~:左侧字符串是否能够被右侧的PATTERN所匹配; ...
$./test.shThetwostringsarenotequal. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。 #!/bin/bashstring1="apples"string2="oranges"if["$string1"!="$string2"];thenecho"Stringsaredifferent."elseecho"Stringsarenotdifferent."fi
The two strings are not equal. 1. 2. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。 #!/bin/bash string1="apples" string2="oranges" if [ "$string1" != "$string2" ]; then echo "Strings are different." else echo "Strings are not different." ...
echo "$Device is not exist" fi 三、bash条件测试 1.整数测试 2.字符测试 3.文件测试 整数测试:数值1 比较符号数值2 比较符号: -ge:相等(equal) -ne:不等(not equal) -gt:大于(greater than) -lt:小于(less than) -ge:大于等于(greater than or equal) ...