執行算數運算就離不開各類運算符號,和其餘編程語言同樣,shell腳本也有運算符號。常見運算符號以下圖所示:web
上圖中的運算符號經常使用於常見的運算命令,經常使用運算命令以下圖所示:shell
雙小括號「(())」的做用是進行數值運算與數值比較,它的效率很高。express
案例1:利用「(())」進行簡單的運算編程
[root@shellbiancheng ~]# echo $((2+4)) 6 [root@shellbiancheng ~]# echo $((2+5)) 7 [root@shellbiancheng ~]# echo $((2-5)) -3 [root@shellbiancheng ~]# ((i=4)) [root@shellbiancheng ~]# ((i=i*5)) [root@shellbiancheng ~]# echo $i 20
案例2:利用「(())」進行稍微複雜的總和運算bash
[root@shellbiancheng ~]# ((a=1+2**4-4%3)) [root@shellbiancheng ~]# echo $a 16 [root@shellbiancheng ~]# b=$((1+2**4-4%3)) [root@shellbiancheng ~]# echo $b 16 [root@shellbiancheng ~]# echo $((1+2**4-4%3)) 16 [root@shellbiancheng ~]# a=$((100*(100+1)/2)) 利用數學公式計算1到100的和 [root@shellbiancheng ~]# echo $a 5050 [root@shellbiancheng ~]# echo $((100*(100+1)/2)) 5050
案例3:利用「(())」雙括號進行比較及判斷編程語言
[root@shellbiancheng ~]# echo $((1>2)) 0 [root@shellbiancheng ~]# echo $((1<2)) 1 [root@shellbiancheng ~]# echo $((1==1)) 1 [root@shellbiancheng ~]# if ((1<2&&1==1)) > then > echo yes > fi yes
提示:雙小括「(())」處理的運算必須是整數(整型),不能是浮點型(小數)或字符串。bc命令和awk命令能夠用於浮點型(小數)運算。ide
案例4:在變量先後使用--和++特殊運算符的表達式工具
a++表示先輸出a的值在進行自增運算(即a++至關於a=a+1) [root@shellbiancheng ~]# a=10 [root@shellbiancheng ~]# echo $((a++)) 10 [root@shellbiancheng ~]# echo $a 11 ++a表示先進行自增運算,在輸出a的值 [root@shellbiancheng ~]# a=10 [root@shellbiancheng ~]# echo $((++a)) 11 [root@shellbiancheng ~]# echo $a 11 a--表示先輸出a的值,再進行遞減運算 [root@shellbiancheng ~]# a=10 [root@shellbiancheng ~]# echo $((a--)) 10 [root@shellbiancheng ~]# echo $a 9 --a表示先進行遞減預算再輸出a的值 [root@shellbiancheng ~]# a=10 [root@shellbiancheng ~]# echo $((--a)) 9 [root@shellbiancheng ~]# echo $a 9
案例5:各類「(())」雙中括號的運算的shell腳本示例lua
[root@shellbiancheng jiaobenlianxi]# cat shuzhijisuan.sh #!/bin/bash a=$1 b=$2 if [ $# -eq 2 ];then echo "a+b=$(($a+$b))" echo "a-b=$(($a-$b))" echo "a*b=$(($a*$b))" echo "a/b=$(($a/$b))" echo "a**b=$(($a**$b))" echo "a%b=$(($a%$b))" else echo $"Usage:Please enter two integers for example $0{2|1}" fi
執行結果以下:url
[root@shellbiancheng jiaobenlianxi]# sh shuzhijisuan.sh 2 1 a+b=3 a-b=1 a*b=2 a/b=2 a**b=2 a%b=0
let運算命令的語法格式爲:let 賦值表達式
let賦值表達式的功能至關於:((賦值表達式))
範例1:給自變量加1
[root@shellbiancheng ~]# i=3 [root@shellbiancheng ~]# i=i+1 開頭先不用let賦值 [root@shellbiancheng ~]# echo $i 輸出時發現打印結果爲i+1,沒有計算 i+1 [root@shellbiancheng ~]# unset i [root@shellbiancheng ~]# i=3 [root@shellbiancheng ~]# let i=i+1 採用let賦值在輸出 [root@shellbiancheng ~]# echo $i 4
提示:let i=i+1至關於((i=i+1)),但雙小括號的方式效率更高。
範例2:監控web服務狀態,若是訪問兩次均失敗,則報警。
[root@linzhongniao scripts]# cat checkurl_let.sh #!/bin/bash function CheckUrl(){ timeout=5 fails=0 success=0 while true do wget --timeout=$timeout --tries=1 ww.baidu.cm -q -O /dev/null if [ $? -ne 0 ];then let fails=fails+1 else let success+=1 fi if [ $success -ge 1 ];then echo success exit 0 fi if [ $fails -ge 2 ];then Critical="sys is down.." echo $Critical|mail -s "$Critical" xxxxxxx@163.com exit 2 fi done } CheckUrl
expr(evaluate expressions求職表達式)命令既能夠用於整數運算,也能夠用於相關字符串長度、匹配等的運算處理。
(1)expr用於計算
語法:expr Expression
範例1:expr運算命令用法
[root@linzhongniao ~]# expr 1+1 1+1 [root@linzhongniao ~]# expr 1 + 1 2 [root@linzhongniao ~]# expr 1 - 1 0 [root@linzhongniao ~]# expr 1 * 1 expr: syntax error [root@linzhongniao ~]# expr 1 \* 1 1 [root@linzhongniao ~]# expr 1 / 1 1
提示:
1.運算符及用於計算的數字兩端必須有空格,不然會有錯誤。
2.使用乘號時,必須用反斜線將其轉義。
(2)expr配合變量計算
[root@linzhongniao ~]# i=2 [root@linzhongniao ~]# i=`expr $i + 1` You have new mail in /var/spool/mail/root [root@linzhongniao ~]# echo $i 3
(1)判斷一個變量或字符串是否爲整數的方法
能夠利用expr作計算是變量或字符串必須是整數的規則,把一個變量或字符串和一個已知的整數(非0)相加,看命令返回值是否爲0,若是爲0就認爲作加法的變量或字符串爲整數,不然不是整數。
範例1:經過expr判斷變量或字符串是否爲整數
[root@shellbiancheng jiaobenlianxi]# i=4 [root@shellbiancheng jiaobenlianxi]# expr $i + 1 &>/dev/null [root@shellbiancheng jiaobenlianxi]# echo $? 0 [root@shellbiancheng jiaobenlianxi]# i=asd [root@shellbiancheng jiaobenlianxi]# expr $i + 1 &>/dev/null [root@shellbiancheng jiaobenlianxi]# echo $? 2
範例2:經過read讀入的方式持續等待輸入,判斷輸入的值是否爲整數
[root@shellbiancheng jiaobenlianxi]# cat read_expr.sh #!/bin/bash while true do read -p "pls input:" a expr $a + 0 >/dev/null 2>&1 [ $? -eq 0 ] && echo int || echo chars done
範例3:修改雙小括號的案例5,要求可以判斷輸入的參數的個數和輸入的參數是否爲整數
[root@shellbiancheng jiaobenlianxi]# cat shuzhijisuan.sh #!/bin/bash a=$1 b=$2 expr $a + $b + 1 >/dev/null 2>&1 if [ $? -eq 0 -a $# -eq 2 ];then echo "a+b=$(($a+$b))" echo "a-b=$(($a-$b))" echo "a*b=$(($a*$b))" echo "a/b=$(($a/$b))" echo "a**b=$(($a**$b))" echo "a%b=$(($a%$b))" else echo $"Usage:Please enter two integers for example num1 num2" fi
(2)expr判斷文件擴展名是否符合要求
範例1:經過expr判斷文件擴展名是否符合要求
[root@shellbiancheng jiaobenlianxi]# cat expr1.sh #!/bin/bash if expr "$1" : ".*\.txt" &>/dev/null then echo "you are using $1" else echo "pls use *.txt file" fi
(3)經過expr計算字符串的長度
[root@shellbiancheng ~]# char="I am linzhongniao" [root@shellbiancheng ~]# expr length "$char" 17 [root@shellbiancheng ~]# echo ${#char} 17 [root@shellbiancheng ~]# echo ${char}|wc -L 17 [root@shellbiancheng ~]# echo ${char}|awk '{print length($0)}' 17
bc是UNIX/Linux下的計算器,所以除了能夠用做計算器來使用,還能夠做爲命令行計算工具使用。
範例1:bc命令作計算器使用
[root@shellbiancheng ~]# bc bc 1.06.95 Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY. For details type `warranty'. 1+1 2
範例2:將bc用在命令行實現計算功能
[root@shellbiancheng ~]# echo 1+1|bc 2 [root@shellbiancheng ~]# echo 1.1+1|bc 2.1 [root@shellbiancheng ~]# echo 6.2-1.1|bc 5.1 [root@shellbiancheng ~]# echo "scale=6;355/113"|bc 使用scale保留六位小數 3.141592 [root@shellbiancheng ~]# echo "scale=7;355/113"|bc 3.1415929 [root@shellbiancheng ~]# i=2 [root@shellbiancheng ~]# i=`echo $i+6|bc` [root@shellbiancheng ~]# echo $i 8
提示:整數運算能夠用「(())」、let、expr等,若是是小數能夠bc或awk,更推薦使用awk。
awk計算使用小數和整數,特別是命令行計算,尤爲是小數很精確,示例以下:
[root@shellbiancheng ~]# echo "2.3 4.5"|awk '{print ($1-$2)}' -2.2 $1爲第一個數字,$2爲第二個數字用空格分開 [root@shellbiancheng ~]# echo "2.3 4.5"|awk '{print ($1+$2)}' 6.8 [root@shellbiancheng ~]# echo "2.3 4.5"|awk '{print ($1*$2)}' 10.35 [root@shellbiancheng ~]# echo "2.3 4.5"|awk '{print ($1/$2)}' 0.511111 [root@shellbiancheng ~]# echo "2.3 4.5"|awk '{print ($1+1)/$2}' 0.733333
[root@shellbiancheng ~]# declare -i a=16 b=7 -i參數能夠將變量定義爲××× [root@shellbiancheng ~]# a=a+b [root@shellbiancheng ~]# echo $a 23
[root@shellbiancheng ~]# i=2 [root@shellbiancheng ~]# i=$[i+2] [root@shellbiancheng ~]# echo $i 4 [root@shellbiancheng ~]# echo $[2+2] 4 [root@shellbiancheng ~]# echo $[2*2] 4 [root@shellbiancheng ~]# echo $[2/2] 1 [root@shellbiancheng ~]# declare A=3 B=7 [root@shellbiancheng ~]# declare C=`expr $A + $B` [root@shellbiancheng ~]# echo $C 10
shell變量除了能夠直接賦值和腳本傳參外,還可使用read命令從標準輸入中得到。read爲內置命令,可經過help read查看幫助。
語法格式:
read [參數] [變量名]
經常使用參數以下:
-p(prompt):設置提示信息
-t(timeout):設置輸入等待的時間,單位默認爲秒
範例1:read實現基本讀入功能
[root@shellbiancheng ~]# read -p "pls input one number:" num pls input one number:1 [root@shellbiancheng ~]# echo $num 1 [root@shellbiancheng ~]# read -p "pls input two number:" num1 num2 pls input two number:1 2 [root@shellbiancheng ~]# echo $num1 1 [root@shellbiancheng ~]# echo $num2 2
提示:read讀入至關於交互式接收用戶輸入,而後給變量賦值
範例2:將「(())」雙括號變量計算中的案例5改爲read讀入的方式
[root@shellbiancheng jiaobenlianxi]# cat shuzhijisuan.sh #!/bin/bash read -p "pls input two number:" a b expr $a + $b + 1 >/dev/null 2>&1 if [ $? -eq 0 ];then echo "a+b=$(($a+$b))" echo "a-b=$(($a-$b))" echo "a*b=$(($a*$b))" echo "a/b=$(($a/$b))" echo "a**b=$(($a**$b))" echo "a%b=$(($a%$b))" else echo $"Usage:Please enter two integers for example num1 num2" fi