The shell supports a variety of operators:
* 算数运算符* 关系运算符* 布尔运算符* 字符串运算符* 文件测试运算符
Arithmetic operators
+ 加 `expr $a + $b` 结果为 30- 减 `expr $a - $b` 结果为 -10* 乘 `expr $a \* $b` 结果为 200/ 除 `expr $b / $a` 结果为 2% 取余 `expr $b % $a` 结果为 0= 赋值 a=$b 将把变量 b 的值赋给 a== 相等,用于比较2个数字 [ $a == $b ] 返回 false!= 不想等,用于比较2个数字 [ $a != $b ] 返回 true
#shell#!/bin/sha=4b=2c=3echo "$a+$b="`expr $a + $b`echo "$a-$b="`expr $a - $b`echo "$a*$b="`expr $a \* $b`echo "$a/$b="`expr $a / $b`echo "$a%$b="`expr $a % $c`if [ $a == $b ];then echo "$a=$b"else echo "$a!=$b"fiif [ $a != $c ];then echo "$a!=$c"else echo "$a=$c"fi
4+2=6
4-2=2
42=8
4/2=2
4%2=1
4!=2
4!=3*
Attention:
- * Number must be added before multiplication can be achieved
- The shell expr syntax in Mac is: $ ((expression)), where the * does not need to be translated
#shell#!/bin/sha=4b=2c=3echo "$a+$b="`expr $(($a+$b))`echo "$a-$b="`expr $(($a-$b))`echo "$a*$b="`expr $(($a*$b))`echo "$a/$b="`expr $(($a/$b))`echo "$a%$b="`expr $(($a%$c))`
4+2=6
4-2=2
42=8
4/2=2
4%2=1*
Relational operators
关系运算符只支持数字,不支持字符串,除非字符串的值是数字-eq 检测两个数是否相等,相等返回 true。 [ $a -eq $b ] -ne 检测两个数是否不相等,不相等返回 true。 [ $a -ne $b ] -gt 检测左边的数是否大于右边的,如果是,则返回 true。 [ $a -gt $b ] -lt 检测左边的数是否小于右边的,如果是,则返回 true。 [ $a -lt $b ] -ge 检测左边的数是否大于等于右边的,如果是,则返回 true。 [ $a -ge $b ] -le 检测左边的数是否小于等于右边的,如果是,则返回 true。
#shell#!/bin/sha=10b=4if [ $a -gt $b ];then echo "$a>$b"else echo "$a<$b"fiif [ $a -ge $b ];then echo "$a>=$b"else echo "$a<$b"fiif [ $a -lt $b ];then echo "$a<$b"else echo "$a>=$b"fiif [ $a -le $b ];then echo "$a<=$b"else echo "$a>$b"fiif [ $a -eq $b ];then echo "$a=$b"else echo "$a!=$b"fiif [ $a -ne $b ];then echo "$a!=$b"else echo "$a=$b"fi
10>4
10>=4
10>=4
10>4
10!=4
10!=4
Boolean operator
! 非,表达式为 true 则返回 false,否则返回 true。 [ ! false ] 返回 true。-o 或,有一个表达式为 true 则返回 true。 [ 10 -lt 20 -o 20 -gt 100 ] 返回 true。-a 与,两个表达式都为 true 才返回 true。 [ 10 -lt 20 -a 20 -gt 100 ] 返回 false。
#shella=10b=20if [ $a != $b ]then echo "$a != $b : a 不等于 b"else echo "$a != $b: a 等于 b"fiif [ $a -lt 100 -a $b -gt 15 ]then echo "$a 小于 100 且 $b 大于 15 : 返回 true"else echo "$a 小于 100 且 $b 大于 15 : 返回 false"fiif [ $a -lt 100 -o $b -gt 100 ]then echo "$a 小于 100 或 $b 大于 100 : 返回 true"else echo "$a 小于 100 或 $b 大于 100 : 返回 false"fiif [ $a -lt 5 -o $b -gt 100 ]then echo "$a 小于 5 或 $b 大于 100 : 返回 true"else echo "$a 小于 5 或 $b 大于 100 : 返回 false"fi
Ten! = 20:a Not equal to B
10 is less than 100 and 20 is greater than 15: Returns True
10 is less than 100 or 20 is greater than 100: Returns True
10 less than 5 or 20 greater than 100: returns false
logical operators
&& 逻辑的 AND [[ $a -lt 100 && $b -gt 100 ]] 返回 false|| 逻辑的 OR [[ $a -lt 100 || $b -gt 100 ]] 返回 true
$shella=10b=20if [[ $a -lt 100 && $b -gt 100 ]]then echo "返回 true"else echo "返回 false"fiif [[ $a -lt 100 || $b -gt 100 ]]then echo "返回 true"else echo "返回 false"fi
Returns false
Returns True
String operators
= 检测两个字符串是否相等,相等返回 true。 [ $a = $b ] 返回 false。!= 检测两个字符串是否相等,不相等返回 true。 [ $a != $b ] 返回 true。-z 检测字符串长度是否为0,为0返回 true。 [ -z $a ] 返回 false。-n 检测字符串长度是否为0,不为0返回 true。 [ -n "$a" ] 返回 true。str 检测字符串是否为空,不为空返回 true。 [ $a ] 返回 true。
#shella="abc"b="efg"if [ $a = $b ]then echo "$a = $b : a 等于 b"else echo "$a = $b: a 不等于 b"fiif [ $a != $b ]then echo "$a != $b : a 不等于 b"else echo "$a != $b: a 等于 b"fiif [ -z $a ]then echo "-z $a : 字符串长度为 0"else echo "-z $a : 字符串长度不为 0"fiif [ -n "$a" ]then echo "-n $a : 字符串长度不为 0"else echo "-n $a : 字符串长度为 0"fiif [ $a ]then echo "$a : 字符串不为空"else echo "$a : 字符串为空"fi
ABC = Efg:a Not equal to B
ABC! = EFG:A Not equal to B
-Z ABC: string length is not 0
-N ABC: string length is not 0
ABC: string is not empty
File Test Operators
The file test operator is used to detect various properties of a file-B file to detect whether the files are block device files, and if so, returns True. [-B $file] returns FALSE. The-C file detects whether the files are character device files and, if so, returns True. [-C $file] returns false. The-D file detects whether the files are directories and, if so, returns True. [-D $file] returns false. The-F file detects whether files are normal files (neither directories nor device files), and if so, returns True. [-F $file] returns TRUE. The-G file detects if the SGID bit is set and returns True if it is. [-G $file] returns false. The-K file detects whether the files have a sticky bit set (Sticky bit), and returns True if it is. [-K $file] returns false. The-P file detects if the files are named pipes and, if so, returns True. [-P $file] returns false. -U file detects whether the file has a SUID bit set and returns True if it is. [-U $file] returns false. The-R file detects whether the files are readable and, if so, returns True. [-R $file] returns TRUE. The-W file detects whether the files are writable and, if so, returns True. [-W $file] returns TRUE. The-X file detects whether files can be executed and, if so, returns True. [-X $file] returns TRUE. -S file to detect whether the files are empty (the file size is greater than 0) and not NULL to return TRUE. [-S $file] returns TRUE. The-e file detects whether files (including directories) exist and, if so, returns True. [-e $file] returns TRUE. Commonly used is the-d,-f,-r,-w,-x,-e,-s
`#shell #!/bin/sh file="screencap" if [ -e $file ];then echo "$file 存在" else echo "$file 不存在" mkdir -p $file fi if [ -d $file ];then echo "$file 是目录" else echo "$file 不是目录" fi if [ -f $file ];then echo "$file 是文件" else echo "$file 不是文件" touch land.txt fi ls > land.txt file2="land.txt" if [ -r $file2 ];then echo "$file2 可读" else echo "$file2 不可读" fi if [ -w $file ];then echo "$file2 可写" else echo "$file2 不可写" fi if [ -x $file2 ];then echo "$file2 可执行" else echo "$file2 不可执行" fi if [ -s $file2 ];then echo "$file2 长度为0" else echo "$file2 长度不为0" fi if [ $file2 ];then echo "$file2 存在" else echo "$file2 不存在" fi
Screencap exists
Screencap is the directory
Screencap is not a file
Land.txt readable
Land.txt can write
Land.txt not enforceable
Land.txt length is 0
Land.txt exists '
Shell 6 Basic operators