天天看点

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