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...
#!/bin/bash # 示例1:使用 = 进行字符串相等比较 string1="hello" string2="hello" if [ "$string1" = "$string2" ]; then echo "The strings are equal." else echo "The strings are not equal." fi # 示例2:使用 != 进行字符串不相等比较 string1="hello" string2="world" if [ "$stri...
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...
[ STRING1 > STRING2 ] 如果 “STRING1” sorts after “STRING2” lexicographically in the current locale则为真。 [ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than,...
echo "String1 and String2 are not equal." fi if [[ "$String1" == "$String2" ]]; then echo "String1 and String2 are equal." else echo "String1 and String2 are not equal." fi if [[ "$String1" != "$String3" ]]; then ...
/bin/bashstring1="apples"string2="oranges"if["$string1"="$string2"];thenecho"Thetwostringsareequal."elseecho"Thetwostringsarenotequal."fi 这是我们执行脚本时的结果: $./test.shThetwostringsarenotequal. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。
一、使用if-then语句(单分支) 用法: if条件测试;then 语句1 语句2 …… fi 解释:if后面的条件测试返回的状态码为0,则执行程序体中的语句;若返回的状态码为非0,则退出。 条件测试:有多种形式,包括bash命令和“[]”测试,后面都会用到 例1:判断一个文件是否存在,如果存在,则输入该文件已存在,如果存在,则...
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 我们还可以使用运算符来测试两个字符串是否不相等!=。
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." ...
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" ] ...