【第三章】Shell 变量的数值计算

一、算数运算符

shell中常见的算术运算符:

shell中常见的算术命令:

1. 整数运算

方法一:expr 

  • expr命令就既可以用于整数运算,也可以用于相关字符串长度、匹配等的运算处理:
  • expr 1 + 2     +     -    \*    /    
  • expr $num1 + $num2 + - \* / %;

注:计算的数字左右都至少有一个空格,

  乘号需要用反斜线屏蔽其特定含义,因为shell会误读星号。

方法二:$(())
echo $(($num1+$num2)) + - * / %
echo $((num1+num2))
echo $((5-3*2))
echo $(((5-3)*2))
echo $((2**3))

echo $((a++)) 、  echo $((a--))     先输出后加减

echo $((++a))   、 echo $((--a))  先加减后输出

sum=$((1+2)); echo $sum

方法三:$[]
echo $[5+2] + - * / %
echo $[5**2]

方法四:let
let sum=2+3; echo $sum
let i++; echo $i

2. 小数运算

echo "2*4" |bc
echo "2^4" |bc
echo "scale=2;6/4" |bc
awk 'BEGIN{print 1/2}'
echo "print 5.0/2" |python

 二、算数运算的实例

2.1 关于$(())的实例

实例一:运算示例

[root@host-131 ~]# echo $((6+2))     #减法
[root@host-131 ~]# echo $((6-2))      #减法
[root@host-131 ~]# echo $((6*2))      #乘法  
[root@host-131 ~]# echo $((6/2))       #除法
[root@host-131 ~]# echo $((6%2))     #余数
[root@host-131 ~]# echo $((6**2))     #幂运算

  实例二:各种运算shell脚本示例

[root@host-131 ~]# cat vartest01.sh 
#!/bin/bash
a=6
b=2
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))"
[root@host-131 ~]# chmod a+x vartest01.sh 
[root@host-131 ~]# ./vartest01.sh 
a-b=4
a-b=4
a*b=12
a/b=3
a**b=36
a%b=0
[root@host-131 ~]#

实例三:实现输入2个数字进行加、减、乘、除功能的计算器

[root@host-131 ~]# cat var_jisuan.sh 
#!/bin/bash
#+,-,*,/ 2018-06-12
print_useage(){
    printf "Please enter an int\n"
    exit 1
}
read -p "Please input first number:" fnum
if [ -n "`echo $fnum |sed 's/[0-9]//g'`" ];then       #-n选项可以理解为字符串 no zero
    print_useage
fi
read -p "Please input the operators:" op
if [ "${op}" != "+" ]&&[ "$op" != "-" ]&&[ "$op" != "*" ]&&[ "$op" != "/" ];then
    echo "Please use {+|-|*|/}"
    exit 2
fi
read -p "Please input second number: " snum
if [ -n "`echo $snum | sed 's/[0-9]//g'`" ];then
    print_useage
fi
echo "${fnum}${op}${snum}=$((${fnum}${op}${snum}))"
[root@host-131 ~]# chmod a+x var_jisuan.sh 
[root@host-131 ~]# ./var_jisuan.sh 
Please input first number:4
Please input the operators:+
Please input second number: 5
4+5=9
[root@host-131 ~]#

 实例四:高效的计算方法

[root@host-131 ~]# cat var_jisuan02.sh 
#!/bin/bash
read -p "Please input num=num1{+|-|*|/}num2:" num
echo $(($num))
[root@host-131 ~]# chmod a+x var_jisuan02.sh 
[root@host-131 ~]# ./var_jisuan02.sh 
Please input num=num1{+|-|*|/}num2:3*5
15
[root@host-131 ~]# 

或者用如下方法:
[root@host-131 ~]# cat var_jisuan02.sh 
#!/bin/bash
echo $(($1))
[root@host-131 ~]# sh var_jisuan02.sh 4+5
9
[root@host-131 ~]#

 2.2 let运算命令实例

实例一、监控web服务状态,如果访问两次均失败,则报警。

#!/bin/bash
CheckUrl(){    #定义函数
timeout=5      #定义wget访问超时时间
fails=0        #初始化网站访问失败次数为0
success=0      #初始化网站访问成功次数为0
while true    #持续循环
    do
        wget --timeout=$timeout --tries=1 https://www.cnblogs.com/yangleitao/ -q -O /dev/null  #--tries
#增加重试次数。 -q 不显示下载过程   -O 下载并以不同的文件名保存 


        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="system is dowm."
            echo $Critical|tee|mail -s "$Critical" [email protected]
            exit 2

        fi
done
}
CheckUrl #执行函数

猜你喜欢

转载自www.cnblogs.com/yangleitao/p/9172953.html
今日推荐