String1 and String3 are not equal. 这里,如果我们先用 = 运算符比较 String1 和 String2。由于 String1 和 String2 都具有相同的长度,具有相同的字符序列,比较运算符返回 true,因此我们得到 String1 and String2 are equal.. 作为程序中第一个 if-else 块的输出。 同样,在第二个程序中,我们使用 == 运...
if [ "$string1" != "Not MyString" ] then echo "Not Equal Strings" else echo "Stringis equal" fi 在Bash 中检测字符串是否是空值或者空串 和那些个与 C++ 类似的语言不同,在 Bash 脚本中还可以用一个命令来检测一个字符串是否是空值(null)或者空串(empty""): if [ -z "$VAR" ] -z实际上...
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 string1="apples" ...
string1="apples" string2="oranges" if [ "$string1" = "$string2" ]; then echo "The two strings are equal." else echo "The two strings are not equal." fi 这是我们执行脚本时的结果: 代码语言:txt 复制 $ ./test.sh The two strings are not equal. 例2 我们还可以使用运算符来测试两...
#!/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...
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...
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." ...
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" ] 格式3:test "$S1" == "$S2" 格式4:[ "$S1" == "$S2" ] ...
如果相等,则输出"Strings are equal",否则输出"Strings are not equal"。 接下来,让我们来看看如何在Bash中使用正则表达式进行字符串匹配。我们可以使用"=~"符号来进行正则表达式匹配,例如: bash. #!/bin/bash. str="hello world" if [[ $str =~ [0-9] ]]; then. echo "String contains a number" ...
$./test.shThetwostringsarenotequal. 例2 我们还可以使用运算符来测试两个字符串是否不相等!=。 #!/bin/bashstring1="apples"string2="oranges"if["$string1"!="$string2"];thenecho"Stringsaredifferent."elseecho"Stringsarenotdifferent."fi