shell 6基本运算符

1.shell支持多种运算符

* 算数运算符 * 关系运算符 * 布尔运算符 * 字符串运算符 * 文件测试运算符

2.算数运算符

运算符 说明 示例
+ `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
#!/bin/sh
a=4
b=2
c=3
echo "$a+$b="`expr $a + $b`    #4+2=6
echo "$a-$b="`expr $a - $b`    #4-2=2
echo "$a*$b="`expr $a * $b`     #4*2=8
echo "$a/$b="`expr $a / $b`    #4/2=2
echo "$a%$b="`expr $a % $c`    #4%2=1
if [ $a == $b ];then echo "$a=$b";else echo "$a!=$b";fi     #4!=2
if [ $a != $c ];then echo "$a!=$c";else echo "$a=$c";fi    #4!=3

注意:
1. *号前面必须加才能实现乘法运算
2. Mac中shell expr语法是:$((表达式)),此处的乘法表示符*不需要转译

2.1 数学运算的5种方法

```#shell #!/bin/sh a=10 b=5 echo '`expr $a + $b`='`expr $a + $b` #`expr $a + $b`=15 echo '$[ `expr $a - $b` ]='$[ `expr $a - $b` ] #不常用 $[ `expr $a - $b` ]=5 echo '$(expr $a * $b)='$(expr $a * $b) #$(expr $a * $b)=50 echo '$[ $a / $b ]='$[ $a / $b ] #$[ $a / $b ]=2 echo '$(($a+$b))='$(($a+$b)) #$(($a+$b))=15* ```

2.2 使用bc进行小数运算

```#shell var=$(echo "变量1;变量2;变量3...;表达式"|bc) ``` 命令行示例 ```#shell (echo "a=3;b=4.4;a * b"|bc) ``` 脚本示例 ```#shell #!/bin/sh width=`adb shell wm size|awk -F ':|x' '{print $2}'` height=`adb shell wm size|awk -F ':|x' '{print $3}'` x=$(echo "${width} * 0.66"|bc) y=$(echo "${height} * 0.75"|bc) echo $x,$y adb shell input tap $x $y ``` 结果:

3.关系运算符

运算符数字字符串
 [ -eq ] 检测两个数是否相等,相等返回 true。
 ;[ 5 -eq 5 ];echo $? => 0
 ;[ 5 -eq 3 ];echo $? => 1
不支持
 [ -ne ] 检测两个数是否不相等,不相等返回 true。
 [ 5 -ne 5 ];echo $? => 1
 [ 5 -ne 3 ];echo $? => 0
不支持
 [ -gt ] 检测左边的数是否大于右边的,如果是,则返回 true。
 [ 5 -gt 5 ];echo $? => 1
 [ 5 -gt 3 ];echo $? => 0
不支持
 [ -lt ] 检测左边的数是否小于右边的,如果是,则返回 true。
 [ 5 -lt 5 ];echo $? => 1
 [ 5 -lt 3 ];echo $? => 1
不支持
 [ -ge ] 检测左边的数是否大于等于右边的,如果是,则返回 true。
 [ 5 -ge 5 ];echo $? => 0
 [ 5 -ge 3 ];echo $? => 0
不支持
 [ -le ] 检测左边的数是否小于等于右边的,如果是,则返回 true。
 [ 5 -le 5 ];echo $? => 0
 [ 5 -le 3 ];echo $? => 1
不支持
[ > ]不支持,一直返回true
 [ 5 > 5 ];echo $? => 0
 [ 5 > 6 ];echo $? => 0
不支持,一直返回true
 [ "h" > "a" ];echo $? => 0
 [ "h" > "w" ];echo $? => 0
[ >= ]不支持
[ 5 >= 4 ];echo $? => unary operator expected
不支持
[ "h" >= "w" ];echo $? => unary operator expected
[ < ]不支持,一直返回true
 [ 5 < 3 ];echo $? => 0
 [ 5 < 6 ];echo $? => 0
不支持,一直返回true
 [ "h" < "a" ];echo $? => 0
 [ "h" < "w" ];echo $? => 0
[ <= ]不支持
[ 5 <= 4 ];echo $? => unary operator expected
不支持
[ "h" <= "w" ];echo $? => unary operator expected
[ = ]
[ == ]
检测两个数是否相等,相等返回 true。
 [ 5 = 5 ];echo $? => 0
 [ 5 = 3 ];echo $? => 1
[ 5 == 5 ];echo $? => 0
[ 5 == 3 ];echo $? => 1
检测两个字符串是否相等,相等返回 true。
 [ "hello" = "hello" ];echo $? => 0
 [ "hello" = "world" ];echo $? => 1
[ "hello" == "hello" ];echo $? => 0
[ "hello" == "world" ];echo $? => 1
[ != ]检测两个数是否不相等,不相等返回 true。
 [ 5 != 5 ];echo $? => 1
 [ 5 != 3 ];echo $? => 0
检测两个字符串是否不相等,不相等返回 true。
 [ "hello" != "hello" ];echo $? => 1
 [ "hello" != "world" ];echo $? => 0
[[ > ]]检测左边的数是否大于右边的,如果是,则返回 true。
 [[ 5 > 4 ]];echo $? => 0
 [[ 5 > 6 ]];echo $? => 0
检测左边的字符串是否大于右边的,如果是,则返回 true。
 [[ "h" > "a" ]];echo $? => 0
 [[ "h" > "w" ]];echo $? => 1
[[ < ]]检测左边的数是否小于右边的,如果是,则返回 true。
 [[ 5 < 4 ]];echo $? => 1
 [[ 5 < 6 ]];echo $? => 0
检测左边的字符串是否小于右边的,如果是,则返回 true。
 [[ "h" < "a" ]];echo $? => 1
 [[ "h" < "w" ]];echo $? => 0
[[ = ]]
[[ == ]]
检测两个数是否相等,相等返回 true。
 [[ 5 = 5 ]];echo $? => 0
 [[ 5 = 3 ]];echo $? => 1
[[ 5 == 5 ]];echo $? => 0
[[ 5 == 3 ]];echo $? => 1
检测两个字符串是否相等,相等返回 true。
 [[ "hello" = "hello" ]];echo $? => 0
 [[ "hello" = "world" ]];echo $? => 1
[[ "hello" == "hello" ]];echo $? => 0
[[ "hello" == "world" ]];echo $? => 1
[[ != ]] 检测两个数是否不相等,不相等返回 true。
 [[ 5 != 5 ]];echo $? => 1
 [[ 5 != 3 ]];echo $? => 0
检测两个字符串是否不相等,不相等返回 true。
 [[ "hello" != "hello" ]];echo $? => 1
 [[ "hello" != "world" ]];echo $? => 0
((>))检测左边的数是否大于右边的,如果是,则返回 true。
 ((5>4));echo $? => 0
 (( 5 > 6 ));echo $? => 0
不支持,一直返回true。
 (( "h" > "a" ));echo $? => 0
 (("h" > "w"));echo $? => 1
((>=))检测左边的数是否大于右边的,如果是,则返回 true。
 ((5>=4));echo $? => 0
 (( 5 >= 6 ));echo $? => 0
不支持,一直返回true。
 (( "h" >= "a" ));echo $? => 0
 (("h" >= "w"));echo $? => 1
((<))检测左边的数是否小于右边的,如果是,则返回 true。
 ((5 < 4));echo $? => 1
 ((5 < 6));echo $? => 0
不支持,始终返回false。
 (("h" < "a"));echo $? => 1
 (("h" < "w"));echo $? => 1
((<=))检测左边的数是否小于右边的,如果是,则返回 true。
 ((5 <= 4));echo $? => 1
 ((5 <= 6));echo $? => 0
不支持,始终返回true。
 (("h" <= "a"));echo $? => 1
 (("h" <= "w"));echo $? => 1
((==))检测两个数是否相等,相等返回 true。
 ((5 == 5));echo $? => 0
 ((5 == 3));echo $? => 1
不支持,始终返回true。
 (("hello" == "hello"));echo $? => 0
 (("hello" == "world"));echo $? => 0
((!=))检测两个数是否不相等,相等返回 true。
 ((5 != 5 ));echo $? => 1
 ((5 != 3));echo $? => 0
不支持,始终返回false。
 (("hello" != "hello"));echo $? => 1
 (("hello" != "world"));echo $? => 1
```#shell #!/bin/sh a=10 b=4 if [ $a -gt $b ];then echo "$a>$b";else echo "$a<$b";fi #10>4 if [ $a -ge $b ];then echo "$a>=$b";else echo "$a<$b";fi #10>=4 if [ $a -lt $b ];then echo "$a<$b";else echo "$a>=$b";fi #10>=4 if [ $a -le $b ];then echo "$a<=$b";else echo "$a>$b";fi #10>4 if [ $a -eq $b ];then echo "$a=$b";else echo "$a!=$b" ;fi #10!=4 if [ $a -ne $b ];then echo "$a!=$b";else echo "$a=$b";fi #10!=4 if(($a>$b));then echo "$a>$b";else echo "$a<$b";fi #10>4 if(($a<$b));then echo "$a>=$b";else echo "$a<$b";fi #10>=4 if(($a=$b));then echo "$a<$b";else echo "$a>=$b";fi #10>=4 if(($a!=$b));then echo "$a<=$b";else echo "$a>$b";fi #10>4 if(($a>=$b));then echo "$a=$b";else echo "$a!=$b" ;fi #10!=4 if(($a<=$b));then echo "$a!=$b";else echo "$a=$b";fi #10!=4 ```

3.1关系运算符总结

数字 字符串
[ ] -eq、-ne、-gt、-lt、-ge、-le
=、==、!=
=、==、!=
[[ ]] >、<、=、==、!= >、<、=、==、!=
(( )) >、>=、<、<=、==、!= 不支持

4.布尔逻辑运算符

</tr>
<tr>
	<td style="text-align: center;">非</td>
	<td> &#160 <font color="green">! 在[ ] [[ ]]中都能使用</font><br>&#160 [ ! 10 -eq 10 ];echo $?</td>
	<td>&#160 [[ ! $a > 0 ]];echo $? => 1</td>
</tr>
  -a在[ ]中使用
 [ 10 -lt 100 -a 20 -gt 100 ];echo $? => 1
  &&在[[ ]]中使用
  [[ 10 -lt 100 && 20 -gt 100 ]];echo $? => 1
  -o在[ ]中使用
  [ 10 -lt 100 -o 20 -gt 100 ];echo $? => 0
  ||在[[ ]]中使用
  [[ $a -lt 100 || $b -gt 100 ]];echo $? => 0

5. 字符串运算符

字符串比较主要是等或不等,是否为空和使用通配符 字符串对比: 从首字母开始,对比ascii码值,如果首字母一样,再对比第二位...
运算符说明示例
[ = ]
[ == ]
[[ = ]]
[[ = ]]
检测两个字符串是否相等,相等返回 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。
使用正则表达式匹配字符串*表示0或多个字符
?表示单个字符
[[ "xxxx" == x* ]];echo $? => 0
[[ "xxxx" == x??? ]];echo $? => 0
```#shell a="abc" b="efg"

if [ $a = (b ];then echo ")a = (b:相等";else echo ")a = $b: 不等";fi #abc = efg: 不等
if [ $a != (b ];then echo ")a != $b : 不等";else echo "是否不想等: 相等";fi #abc != efg : 不等
if [ -z $a ];then echo "-z $a : 字符串长度为 0";else echo "-z (a : 字符串长度不为 0" ;fi #-z abc : 字符串长度不为 0 if [ -n ")a" ];then echo "-n $a : 字符串长度不为 0";else echo "-n $a : 字符串长度为 0";fi #-n abc : 字符串长度不为 0
if [ (a ];then echo ")a : 字符串不为空";else echo "$a : 字符串为空";fi #abc : 字符串不为空


<h1>6. 文件测试运算符</h1>
    文件测试运算符用于检测文件的各种属性,常用的是-d,-f,-r,-w,-x,-e,-s
<table border="1">
  <tr>
    <td style="text-align: center;">运算符</td>
    <td>说明</td>
    <td>示例</td>
  </tr>
  <tr>
    <td style="text-align: center;">-d file</td>
    <td>检测文件是否是目录,如果是,则返回 true。</td>
    <td>[ -d $file ] 返回 false。</td>
  </tr> 
  <tr>
    <td style="text-align: center;">-f file </td>
    <td>检测文件是否是普通文件(既不是目录,也不是设备文件),如果是,则返回 true。</td>
    <td>[ -f $file ] 返回 true。</td>
  </tr>
<tr>
    <td style="text-align: center;">-r file </td>
    <td> 检测文件是否可读,如果是,则返回 true。</td>
    <td>[ -r $file ] 返回 true。</td>
  </tr>
  <tr>
    <td style="text-align: center;">-w file </td>
    <td>检测文件是否可写,如果是,则返回 true。</td>
    <td>[ -w $file ] 返回 true。</td>
  </tr>
  <tr>
    <td style="text-align: center;">-x file  </td>
    <td>检测文件是否可执行,如果是,则返回 true。</td>
    <td>[ -x $file ] 返回 true。</td>
  </tr>
  <tr>
    <td style="text-align: center;">-e file</td>
    <td>检测文件(包括目录)是否存在,如果是,则返回 true。</td>
    <td>[ -e $file ] 返回 true。</td>
  </tr>
  <tr>
    <td style="text-align: center;">-s file</td>
    <td>检测文件是否为空(文件大小是否大于0),不为空返回 true.</td>
    <td>[ -s $file ] 返回 true。</td>
  </tr>
  <tr>
    <td style="text-align: center;">-b file</td>
    <td> 检测文件是否是块设备文件,如果是,则返回 true。 </td>
    <td> [ -b $file ] 返回 false。</td>
  </tr>
  <tr>
    <td style="text-align: center;">-c file</td>
    <td>检测文件是否是字符设备文件,如果是,则返回 true。</td>
    <td>[ -c $file ] 返回 false。</td>
  </tr>
  <tr>
    <td style="text-align: center;">-g file</td>
    <td>检测文件是否设置了 SGID 位,如果是,则返回 true。</td>
    <td>[ -g $file ] 返回 false。</td>
  </tr>
  <tr>
    <td style="text-align: center;">-k file</td>
    <td>检测文件是否设置了粘着位(Sticky Bit),如果是,则返回 true。</td>
    <td>[ -k $file ] 返回 false。</td>
  </tr>
  <tr>
    <td style="text-align: center;">-p file</td>
    <td>检测文件是否是有名管道,如果是,则返回 true。</td>
    <td>[ -p $file ] 返回 false。</td>
  </tr>
  <tr>
    <td style="text-align: center;">-u file </td>
    <td>检测文件是否设置了 SUID 位,如果是,则返回 true。</td>
    <td>[ -u $file ] 返回 false。</td>
  </tr> 
</table>
```#shell
#!/bin/sh
file="screencap"
if [ -e $file ];then echo "$file 存在";else echo "$file 不存在,创建";mkdir -p $file;fi    #screencap 存在
if [ -d $file ];then echo "$file 是目录";else echo "$file 不是目录";fi    #screencap 是目录
if [ -f $file ];then echo "$file 是文件";else echo "$file 不是文件";fi    #screencap 不是文件
ls > land.txt
file2="land.txt"
if [ -r $file2 ];then echo "$file2 可读";else echo "$file2 不可读";fi    #land.txt 可读
if [ -w $file ];then echo "$file2 可写";else echo "$file2 不可写";fi    #land.txt 可写
if [ -x $file2 ];then echo "$file2 可执行";else echo "$file2 不可执行";fi    #land.txt 不可执行
if [ -s $file2 ];then echo "$file2 不为空";else echo "$file2 为空";fi    #land.txt 不为空
if [ $file2 ];then echo "$file2 存在";else echo "$file2 不存在";fi    #land.txt 存在
原文地址:https://www.cnblogs.com/csj2018/p/9555911.html