Shell指令碼(2)
Shell變數數值計算
shell中的算術運算子
shell中的運算命令
算術運算子常用於運算命令,shell中運算命令有以下幾種:
示例1,(())命令實踐
雙小括號的作用是進行數值運算與數值的比較,常用。
[root@moli_linux1 shell_test] echo $((1+1)) #計算1+1後輸出 2 [root@moli_linux1 shell_test] echo $((9-3)) 6 [root@moli_linux1 shell_test] ((i=5)) [root@moli_linux1 shell_test] echo $((i+=1)) #獲取i值,計算i+1,把i+1重新賦給i,輸出i 6 [root@moli_linux1 shell_test] echo $i 6 [root@moli_linux1 shell_test] ((a=1+2**3-4%3)) #<==表示式運算後將結果賦值給變數a,先乘除後加減 [root@moli_linux1 shell_test] echo $((1+2**3-4%3)) #<==這裡直接將運算表示式進行運算並將結果輸出,輸出要加上$符號 8 [root@moli_linux1 shell_test] b=$((1+2**3-4%3)) #<==這裡直接將運算表示式進行運算並將結果輸出,輸出結果賦值給變數b [root@moli_linux1 shell_test]# echo $b 8 [root@moli_linux1 shell_test] a=$((100*(100+1)/2)) #<==利用公式計算1+2+3....100 [root@moli_linux1 shell_test] echo $a 5050
示例2,用(())命令進行比較
[root@moli_linux1 shell_test] echo $((3>6)) #<==3>6是不成立的,因此輸出0。表示比較時,1為真,0為假 0 [root@moli_linux1 shell_test] echo $((3>1)) #<==3>1是成立的,因此輸出1. 1 [root@moli_linux1 shell_test] echo $((3==3)) #<==3=3是成立的,因此輸出1 1
注意:上面提到的數字和變數必須是整型的,不能是浮點型或字串。下面的bc和awk命令可以用於進行浮點數運算。
[root@moli_linux1 ~] echo $((a=1.0+2.5)) -bash: a=1.0+2.5: 語法錯誤: 無效的算術運算子 (錯誤符號是 ".0+2.5") [root@moli_linux1 ~]#
示例3,有關++,--的運算
[root@moli_linux1 shell_test] a=10 [root@moli_linux1 shell_test] echo $((a++)) 變數a在運算子++之前,輸出整個表示式時會輸出a的值,再進行表示式的自增 10 [root@moli_linux1 shell_test] echo $a 此時輸出a變數的值,是自增後的值,即為11 11 [root@moli_linux1 shell_test] echo $((a--)) 11 [root@moli_linux1 shell_test] echo $a 10 [root@moli_linux1 shell_test] echo $((++a)) 變數a在運算子++之後,輸出整個表示式時會先輸出整個表示式自增的值,即11 11 [root@moli_linux1 shell_test] echo $a 11 [root@moli_linux1 shell_test] echo $((--a)) 10 [root@moli_linux1 shell_test] echo $a 10
變數在++.--運算子之前,輸出表達式的值為變數自身,再進行表示式的自增或自減;變數在++.--運算子之後,會先進行表示式的自增或自減,再輸出表達式的值。
let運算命令
let運算命令的語法格式為:let 賦值表示式
let賦值表示式相當於”((賦值表示式))“
[root@moli_linux1 shell_test] i=5 [root@moli_linux1 shell_test] let i=i+1 [root@moli_linux1 shell_test] echo $i 6
expr運算命令
語法:expr 表示式
[root@moli_linux1 shell_test] expr 2 + 2 4 [root@moli_linux1 shell_test] expr 2 - 2 0 [root@moli_linux1 shell_test] expr 2 * 2 expr: 語法錯誤 [root@moli_linux1 shell_test] expr 2 \* 2 4 [root@moli_linux1 shell_test] expr 2 / 2 1
注意:使用expr時:
- 運算子及用於計算的數字左右至少有一個空格,否則會報錯
- 使用乘號時,必須使用反斜線遮蔽其特定含義,因為shell可能會誤解星號的含義
expr在Shell中可配合變數進行計算,但需要用反引號將計算表示式括起來
[root@moli_linux1 shell_test] i=5 [root@moli_linux1 shell_test] i=` expr $i + 6 ` 此處反引號括起來的表示式,變數和數字符號兩邊都要有空格 [root@moli_linux1 shell_test] echo $i 11
Shell指令碼中常見的需求
判斷一個未知的變數是不是整數
原理: 利用expr做計算時變數或字串必須是整數的規則,把一個變數和一個整數(非零)相加。看命令的返回值是否為0。如果為0,就認為與整數相加的變數是整數,如果不是0,就不是整數
[root@moli_linux1 shell_test] i=5 #此時的i是整數,數字5 [root@moli_linux1 shell_test] expr $i + 6 &>/dev/null #把i與一個整數相加,&>/dev/null 表示不保留任何輸出 [root@moli_linux1 shell_test] echo $? 0 #返回0說明i是整數 [root@moli_linux1 shell_test] i=moli [root@moli_linux1 shell_test] expr $i + 6 &>/dev/null [root@moli_linux1 shell_test] echo $? 2 #返回1說明i不是是整數
判斷傳參是不是整數
[root@moli_linux1 shell_test] cat t3.sh #!/bin/bash expr $1 + 1 >/dev/null 2>&1 [ $? -eq 0 ] &&echo int||echo chars #這是一個條件表示式語法,返回值為0則輸出int,否則輸出chars [root@moli_linux1 shell_test] sh t3.sh 1 int [root@moli_linux1 shell_test] sh t3.sh a chars