/bin/bashn=4if![[$n-eq0]];thenecho"Not equal to 0"fi 输出: Not equal to0
输出: Not equal 表达式中的 Bash if not 条件 我们可以使用! [[]] 之外的运算符使整个表达式的输出为负数。我们不能在双方括号内进行操作以使单个表达式为负数。让我们看一个例子。 #!/bin/bash n=4 if ! [[ $n -eq 0 ]]; then echo "Not equal to 0" fi 输出: Not equal to 0 相关...
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...
-ne:不等于(not equal to) 下面是一个数字比较的例子: 代码语言:txt 复制 num1=10 num2=20 if [ $num1 -gt $num2 ]; then echo "num1 is greater than num2" else echo "num1 is less than or equal to num2" fi 对于字符串的比较,在Bash中可以使用以下的比较操作符: ...
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 ...
==: Equal to !=: Not equal to -z: Empty string -n: Non-empty string 3. File and Directory Checks: -e: Exists -f: Regular file -d: Directory -r: Readable -w: Writable -x: Executable Example of using If Statement: Let’s illustrate the usage of if statements with a few examples...
译:除了在内置命令set的文档(man -a set)里描述的shell选项,bash还可支持以下选项-cstringIf the -c option is present,thencommands are read fromstring. If there are arguments after thestring, they are assigned to the positional parameters, starting ...
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. 判断文件 上面的代码是判断文件是否存在、是否是常规文件和是否是目录。如果条件成立,则输出对应...
= STRIN2 # Trueifstrings arenotequal#算术测试操作var1 -eq var2 # Trueifvar1 is equal to var2var1 -ne var2 # Trueifvar1notequal to var2var1 -lt var2 # Trueifvar1 is less than var2var1 -le var2 # Trueifvar1 is less thanorequal ...
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