[ 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, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “AR...
-ne——不等于(not equal) -lt——小于(less than) -le——小于等于(less equal) -gt——大于(great than) -ge——大于等于(great equal) -f——文件(file) -d——目录(directory) (一)选择执行语句 单分支的if语句 if 测试条件;then fi 双分支的if语句 if 测试条件;then 如果满足条件就执行这里的...
#!/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...
/bin/bash#Device=/dev/sda3ifmount|grep"$Device"&>/dev/null;thengrep"$Device"|cut-d""-f3elseecho"$Deviceis not exist"fi 1. 2. 3. 4. 5. 6. 7. 8. 三、bash条件测试 1.整数测试 2.字符测试 3.文件测试 整数测试:数值1比较符号数值2 比较符号: -ge:相等(equal) -ne:不等(not equal...
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...
A -ne B: 不等于 not equal to 2.3 字符串测试:A, B A > B A < B A >= B A <= B A == B或A = B:等值比较 A != B: 不等于 -z A: 判断A是否为空;空则为真,不空则假; -n A:判断A是否不空;不空则为真,空则为假; =~ "$A" =~ PATTERN 如果变量A中保存的字符串能被PATTERN...
[ 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, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “AR...
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
if [ "$str1" = "$str2" ]; then echo "Strings are equal." else echo "Strings are not equal." fi 输出结果 Strings are equal. 在bash中为何\n等于'n'? bash 关注收藏 BETA 在bash中,\n并不等于'n'。在你的示例中,出现Strings are equal.的输出,是因为在bash字符串比较中的特殊行为,但这并...
因为在中国人的世界里,电脑键盘上面的使用shift才能输出来的字符从小就不太认识,但是在bash脚本中是必须要用的。 下面就是一个逻辑判断,变量定义的语句。仅仅是demo。没有实际作用。 #!/bin/sh#fordemo purpose only a=2b=2if[ $a =$b ]thenecho"eq"#equalelseecho"not eq"#not equalfi...