天天看點

shell腳本變量數值計算1.算數運算符2.雙小括号“(())”運算指令3.let運算指令4.expr指令的用法5.bc指令用法6.awk計算7.declare指令的用法8.$[ ]符号的運算示例9.基本shell變量輸入read指令的運算實戰

1.算數運算符

執行算數運算就離不開各種運算符号,和其他程式設計語言一樣,shell腳本也有運算符号。常見運算符号如下圖所示:

shell腳本變量數值計算1.算數運算符2.雙小括号“(())”運算指令3.let運算指令4.expr指令的用法5.bc指令用法6.awk計算7.declare指令的用法8.$[ ]符号的運算示例9.基本shell變量輸入read指令的運算實戰

上圖中的運算符号常用于常見的運算指令,常用運算指令如下圖所示:

shell腳本變量數值計算1.算數運算符2.雙小括号“(())”運算指令3.let運算指令4.expr指令的用法5.bc指令用法6.awk計算7.declare指令的用法8.$[ ]符号的運算示例9.基本shell變量輸入read指令的運算實戰

2.雙小括号“(())”運算指令

2.1 雙小括号數值運算的基礎文法

雙小括号“(())”的作用是進行數值運算與數值比較,它的效率很高。

shell腳本變量數值計算1.算數運算符2.雙小括号“(())”運算指令3.let運算指令4.expr指令的用法5.bc指令用法6.awk計算7.declare指令的用法8.$[ ]符号的運算示例9.基本shell變量輸入read指令的運算實戰

2.2 雙小括号數值運算案例

案例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:利用“(())”進行稍微複雜的總和運算

[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指令可以用于浮點型(小數)運算。

案例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腳本示例

[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           

執行結果如下:

[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           

3.let運算指令

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" [email protected]
exit 2
fi
done
}
CheckUrl           

4.expr指令的用法

4.1 expr指令的基本用法示例

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           

4.2 expr實戰案例

(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           

5.bc指令用法

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。

6.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           

7.declare指令的用法

[root@shellbiancheng ~]# declare -i a=16 b=7 -i參數可以将變量定義為×××
[root@shellbiancheng ~]# a=a+b
[root@shellbiancheng ~]# echo $a
23           

8.$[ ]符号的運算示例

[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           

9.基本shell變量輸入read指令的運算實戰

9.1 read指令基礎

shell變量除了可以直接指派和腳本傳參外,還可以使用read指令從标準輸入中獲得。read為内置指令,可通過help read檢視幫助。

文法格式:

read [參數] [變量名]

常用參數如下:

-p(prompt):設定提示資訊

-t(timeout):設定輸入等待的時間,機關預設為秒

[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           
[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