1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #!/bin/bash a=10 b=20 val=`expr  $a  + $b ` echo  "a + b : $val " val=`expr  $a  - $b ` echo  "a - b : $val " val=`expr  $a  \* $b ` echo  "a * b : $val " val=`expr  $b  / $a ` echo  "b / a : $val " val=`expr  $b  % $a ` echo  "b % a : $val " if  [ $a  == $b  ]then    echo  "a == b"  fi if  [ $a  != $b  ]then    echo  "a != b"  fi 
运行
1 2 3 4 5 6 7 $bash  test.sha + b : 30 a - b : -10 a * b : 200 b / a : 2 b % a : 0 a != b 
原生 bash 不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 expr,expr 最常用。 
expr 是一款表达式计算工具,使用它能完成表达式的求值操作。注意使用的反引号(esc键下边) 
表达式和运算符之间要有空格 $a + $b 写成 $a+$b 不行 
条件表达式要放在方括号之间,并且要有空格 [ $a == $b ] 写成 [$a==$b] 不行 
乘号(*)前边必须加反斜杠(\)才能实现乘法运算 
 
关系运算符只支持数字,不支持字符串,除非字符串的值是数字。
实例
1 2 3 4 5 6 7 8 9 10 11 #!/bin/bash a=10 b=20 if  [ $a  -eq $b  ]then    echo  "$a  -eq $b  : a == b"  else    echo  "$a  -eq $b : a != b"  fi 
运行
1 2 $bash  test2.sh10 -eq 20: a != b 
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #!/bin/bash a=10 b=20 if  [[ $a  -lt 100 && $b  -gt 100 ]]then    echo  "return true"  else    echo  "return false"  fi if  [[ $a  -lt 100 || $b  -gt 100 ]]then    echo  "return true"  else    echo  "return false"  fi 
结果
1 2 return  false return  true 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 #!/bin/bash a="abc"  b="efg"  if  [ $a  = $b  ]then    echo  "$a  = $b  : a == b"  else    echo  "$a  = $b : a != b"  fi if  [ -n $a  ]then    echo  "-n $a  : The string length is not 0"  else    echo  "-n $a  : The string length is  0"  fi if  [ $a  ]then    echo  "$a  : The string is not empty"  else    echo  "$a  : The string is empty"  fi 
结果
1 2 3 abc = efg: a != b -n abc : The string length is not 0 abc : The string is not empty 
实例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 #!/bin/bash file="/home/shiyanlou/test.sh"  if  [ -r $file  ]then    echo  "The file is readable"  else    echo  "The file is not readable"  fi if  [ -e $file  ]then    echo  "File exists"  else    echo  "File not exists"  fi 
结果
1 2 The file is readable File exists 
浮点运算,比如实现求圆的面积和周长。
expr 只能用于整数计算,可以使用 bc 或者 awk 进行浮点数运算。
 
1 2 3 4 5 6 7 8 9 10 11 12 13 #!/bin/bash raduis=2.4 pi=3.14159 girth=$(echo  "scale=4; 3.14 * 2 * $raduis "  | bc) area=$(echo  "scale=4; 3.14 * $raduis  * $raduis "  | bc) echo  "girth=$girth " echo  "area=$area " 
以上代码如果想在环境中运行,需要先安装 bc。
 
1 2 sudo apt-get update sudo apt-get install bc 
矩形的长 a=3,宽 b=2
创建一个 Area.sh ,能够计算此矩形的面积,输出面积的值
创建一个 Cum.sh ,能够计算此矩形的周长,输出周长的值
文件名一定要一致,以便于验证结果 
文件创建在 /home/shiyanlou/ 下 
 
注意:请务必先独立思考获得 PASS 之后再查看参考代码,直接拷贝代码收获不大 
此题解法不唯一,这里只是给出其中一种作为参考。
/home/shiyanlou/Area.sh 的参考代码:
1 2 3 4 #!/bin/bash a=3 b=2 echo  `expr  $a  \* $b `
/home/shiyanlou/Cum.sh 的参考代码:
1 2 3 4 5 #!/bin/bash a=3 b=2 c=`expr  $a  + $b ` echo  `expr  $c  \* 2`