/bin/bashn=4if![[$n-eq0]];thenecho"Not equal to 0"fi 输出: Not equal to0
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...
dir=$1ifcd"$dir"2>/dev/null#2>/dev/null隐藏了出错提示 then echo"Now in $dir"elseecho"Can't change to $dir"fi9.test或[]的使用,也不一定要有if 例如 #!/bin/bash var1=20var2=22["$var1"-ne"$var2"] && echo"$var1 is not equal to $var2"home=/home [-d $home ] || ech...
-eq: Equal to -ne: Not equal to -gt: Greater than -lt: Less than -ge: Greater than or equal to -le: Less than or equal to 2. String Comparisons: ==: Equal to !=: Not equal to -z: Empty string -n: Non-empty string 3. File and Directory Checks: -e: Exists -f: Regular ...
#!/bin/bash num1=10 num2=20 if [ $num1 -lt $num2 ]; then echo "Number 1 is less than Number 2" fi str1="hello" str2="world" if [ $str1 != $str2 ]; then echo "String 1 is not equal to String 2" fi file="/path/to/file" if [ -e $file ]; then echo "File ...
echo "$a is not equal to $b" 12 echo "(arithmetic comparison)" 13 fi 14 echo 15 # a,b作为字符串进行比较 16 if [ "$a" != "$b" ] 17 then 18 echo "$a is not equal to $b." 19 echo "(string comparison)" 20 fi
if [[ $s1 = "abc" ]]; then echo "\$s1 is equal to \"abc\"" fi 上面的代码是判断字符串是否相等,如果相等,输出$s1 is equal to "abc",否则输出$s2 is not equal to "abc"。 n1=11 n2=10 3. 判断文件 上面的代码是判断文件是否存在、是否是常规文件和是否是目录。如果条件成立,则输出对应...
[ 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...
How to check if a string is in an array? How to use the Bash ternary operator? How to negate an if condition in a Bash if statement? (if not command or if not equal) How to use the BASH_REMATCH variable with the Regular Expression Operator =~?
String Not Equal To [[ $fruit != "banana" ]] ! Not [[ ! "apple" =~ ^b ]] If和Else 条件表达式的功能非常强大,因为我们可以使用它们来控制正在编写的Bash程序的执行方式。Bash编程中的基本构造之一是IF语句。在IF语句中编写的代码只在某个条件为真时执行,否则代码将被跳过。让我们写一个带有IF语句...