运算符
算术 + - * / = == !=
关系 (关系运算符只支持数字) -eq -ne -gt -lt -ge -le
布尔 ! -a -o
字符 = != -z -n str
file -s -e -f -d -r -w -x
算术运算符
expr 必须要有空格
express | des |
---|---|
expr 2 + 2 | |
value= `expr 2 + 2` | |
val=`expr 2 * 5` | 必须加反斜线 |
expr 1 == 1 expr 1 != 1 | false 返回 0,true 返回 1 |
operator | des |
---|---|
+ | + |
- | - |
* | 必须使用\* |
/ | / |
% | % |
= | 赋值 |
== | 相等 |
!= | 不相等 |
关系运算符
关系运算符只支持数字,不支持字符串,除非字符串的值是数字。
operator | des |
---|---|
-eq | is equals,equals return true |
-ne | is not equals,not equals return true |
-gt | > |
-lt | < |
-ge | >= |
-le | <= |
布尔运算符
operator | des | demo | ||
---|---|---|---|---|
! | 非运算 | if [ $a != $b ] | ||
-o | 或运算 or,相当于\ | \ | ,有一个表达式为 true 则返回 true | if [ $a > $b -o $a > $c ] |
-a | 与运算 and,相当于&&,两个表达式都为 true 才返回 true | if [ $a > $b -a $a > $c] |
字符运算符
operator | des | demo |
---|---|---|
= | is equals,这个不是赋值 | if [ $a = $b ] |
!= | is not equals | if [ $a != $b ] |
-z | lenght=0 return true | if [ -z $a ] |
-n | length!=0 return true | if [ -n $a ] |
str | str is not null,return true | if [ $a ] |
文件测试运算符列表
operator | des |
---|---|
-s | is empty |
-e | ie exist |
-d | directory |
-f | 普通文件(既不是目录,也不是设备文件) |
-r | can read |
-w | can write |
-x | can execute |
-b | is block |
-c | is 字符设备文件 |
-g | 是否设置了 SGID 位 |
-k | 设置了粘着位(Sticky Bit) |
-p | 是否是具名管道 |
-u | 是否设置了 SUID 位 |