zoukankan      html  css  js  c++  java
  • SHELL (4) —— 变量的数值计算实践

    摘自:Oldboy Linux运维——SHELL编程实战

    利用(())双括号进行比较及判断:

    [root@yeebian ~]# echo $((3<8))
    1                                   #1表示真。
    [root@yeebian ~]# echo $((8<3))
    0                                   #0表示假。
    [root@yeebian ~]# echo $((8==8))
    1
    [root@yeebian ~]# if ((8>7&&5==5))
    > then
    > echo yes
    > fi
    yes
    

    上面涉及的数字及变量必须为整数(整型),不能是小数(浮点数)或字符串。

    在变量前后使用--和++特殊运算符的表达式。

    [root@yeebian ~]# a=10
    [root@yeebian ~]# echo $((a++))
    10
    [root@yeebian ~]# echo $a
    11
    [root@yeebian ~]# a=11
    [root@yeebian ~]# echo $((a--))
    11
    [root@yeebian ~]# echo $a
    10
    [root@yeebian ~]# a=10
    [root@yeebian ~]# echo $a
    10
    [root@yeebian ~]# echo $((--a))
    9
    [root@yeebian ~]# echo $a
    9
    [root@yeebian ~]# echo $((++a))
    10
    [root@yeebian ~]# echo $a
    10
    

    有关++、--运算的记忆方法:

    变量a在运算符之前,输出表达式的值为a,然后a自增或自减;变量a在运算符之后,输出表达式会先自增或自减,表达式的值就是自增或自减后a的值。

    各种(())运算的Shell脚本示例。

    [root@yeebian ~]# vim test.sh 
    testchars
    #!/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@yeebian ~]# bash test.sh 
    a-b=4
    a+b=8
    a*b=12
    a/b=3
    a**b=36
    a%b=0
    

    由此延伸的脚本:

    [root@yeebian ~]# vim test.sh 
    #!/bin/bash
    a=$1
    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@yeebian ~]# bash test.sh 10 5
    a-b=5
    a+b=15
    a*b=50
    a/b=2
    a**b=100000
    a%b=0
    

    计算字符串长度:

    [root@yeebian ~]# char="I am studying Shell."
    [root@yeebian ~]# expr length "$char"
    20 
    [root@yeebian tmp]# echo ${#char}
    20
    [root@yeebian tmp]# echo $char | wc -L
    20
    [root@yeebian tmp]# echo $char | awk '{print length($0)}'
    20

    其中echo ${#char}这种方式是最快的。

    expr实践

    判断参数个数是否为2,若不是,则给出提示终止运行;

    判断传入的参数是否为整数,若不是,则给出提示终止运行;

    做运算。

    [root@yeebian vurtne]# vim shell.sh
    #!/bin/bash
    [ $# -ne 2 ]&&{
       echo $"USAGE $0 NUM1 NUM2"
       exit 1
    }
    a=$1
    b=$2
    expr $a + $b + 110 &>/dev/null
    if [ $? -ne 0 ]
    then
       echo "You must input two nums."
       exit 2
    fi
    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@yeebian vurtne]# bash shell.sh 1 q
    You must input two nums.
    [root@yeebian vurtne]# bash shell.sh 0
    USAGE shell.sh NUM1 NUM2
    [root@yeebian vurtne]# bash shell.sh 8 2
    a-b=6
    a+b=10
    a*b=16
    a/b=4
    a%b=0
    a**b=64

    通过一条命令计算输出1+2+3+...+10的表达式,并计算结果:

    [root@yeebian ~]# echo `seq -s '+' 10` = `seq -s "+" 10 | bc`
    1+2+3+4+5+6+7+8+9+10 = 55
    [root@yeebian ~]# echo "`seq -s '+' 10` = "$((`seq -s "+" 10`))
    1+2+3+4+5+6+7+8+9+10 = 55
    [root@yeebian ~]# echo `seq -s '+' 10` = `seq -s " + " 10 | xargs expr`
    1+2+3+4+5+6+7+8+9+10 = 55
    [root@yeebian ~]# echo `seq -s "+" 10` = $(echo $[`seq -s "+" 10`])
    1+2+3+4+5+6+7+8+9+10 = 55
    

    awk计算

    [root@yeebian ~]# echo "7.7 3.8" | awk '{print ($1-$2)}'
    3.9
    [root@yeebian ~]# echo "358 113" | awk '{print ($1-3)/$2}'
    3.14159
    [root@yeebian ~]# echo "3 9" | awk '{print ($1+3)*$2}'
    54
    

    read命令实践

    [root@yeebian vurtne]# vim shell.sh 
    #!/bin/bash
    read -p "Please input two num:" a b
    [ ${#a} -le 0 ]&&{
       echo "The first num is null"
       exit 1
    }
    [ ${#b} -le 0 ]&&{
       echo "The second num is null"
       exit 1
    }
    expr $a + 1 &>/dev/null
    RETVAL_A=$?
    expr $b + 1 &>/dev/null
    RETVAL_B=$?
    if [ $RETVAL_A -ne 0 -o $RETVAL_B -ne 0 ]
    then
       echo "One of the num is not num,please input again."
       exit 1
    fi
    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@yeebian vurtne]# bash shell.sh 
    Please input two num:1 qq       
    One of the num is not num,please input again.
    [root@yeebian vurtne]# bash shell.sh 
    Please input two num:qq
    The second num is null
    [root@yeebian vurtne]# bash shell.sh 
    Please input two num:5
    The second num is null
    [root@yeebian vurtne]# bash shell.sh
    Please input two num:5 6 7
    One of the num is not num,please input again.
    [root@yeebian vurtne]# bash shell.sh
    Please input two num:5 7
    a-b=-2
    a+b=12
    a*b=35
    a/b=0
    a%b=5
    a**b=78125
    
  • 相关阅读:
    QT 主窗口和子窗口相互切换示例
    《漫画线性代数》读书笔记 通读小结
    “网购懊悔权”为何与“机动车撞人负全责”一样扯淡?
    第十六周oj刷题——Problem J: 填空题:静态成员---计算学生个数
    HDU 1269 迷宫城堡(强连通)
    HashSet的排序
    android:改动PagerTabStrip中的背景颜色,标题字体的样式、颜色和图标以及指示条的颜色
    Spring中的FactoryBean
    免费APP在线測试工具以及其用法
    freemarker的TemplateExceptionHandler使用
  • 原文地址:https://www.cnblogs.com/vurtne-lu/p/6972566.html
Copyright © 2011-2022 走看看