执行算数运算就离不开各类运算符号,和其余编程语言同样,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