shell編程系列5--數學運算 方法1 expr $num1 operator $num2 方法2 $(($num1 operator $num2)) expr操做符對照表1
操做符 含義 num1 | num2 num1不爲空且非0,返回num1;不然返回num2 num1 & num2 num1不爲空且非0,返回num1;不然返回0 num1 < num2 num1小於num2,返回1;不然返回0 num1 <= num2 num1小於等於num2,返回1;不然返回0 num1 = num2 num1等於num2,返回1;不然返回0 num1 != num2 num1不等於num2,返回1;不然返回0 num1 > num2 num1大於num2,返回1;不然返回0 num1 >= num2 num1大於等於num2,返回1;不然返回0 expr操做符對照表2操做符 含義 num1 + num2 求和 num1 - num2 求差 num1 * num2 求積 num1 / num2 求商 num1 % num2 求餘數 bash數學運算之expr: # 比較大小,只能對整數進行比較 [root@es01 ~]# num1=30 [root@es01 ~]# num2=50 # 錯誤:沒有加空格 [root@es01 ~]# expr $num1>$num2 [root@es01 ~]# echo $? 0 [root@es01 ~]# echo $num2 50 # 錯誤:沒有轉義 [root@es01 ~]# expr $num1 > $num2 # 正確寫法 [root@es01 ~]# expr $num1 \> $num2 0 [root@es01 ~]# num1=100 [root@es01 ~]# echo $num1 100 [root@es01 ~]# expr $num1 \> $num2 1 # 小於、小於等於、大於等於 [root@es01 ~]# expr $num1 \< $num2 0 [root@es01 ~]# expr $num1 \<= $num2 0 [root@es01 ~]# expr $num1 \>= $num2 1 # 運算 加、減、乘、除 [root@es01 ~]# num1=17 [root@es01 ~]# num2=5 [root@es01 ~]# expr $num1 + $num2 22 [root@es01 ~]# num3=`expr $num1 + $num2` [root@es01 ~]# echo $num3 22 [root@es01 ~]# expr $num1 - $num2 12 [root@es01 ~]# expr $num1 \* $num2 85 [root@es01 ~]# expr $num1 / $num2 3 # 取餘數 [root@es01 ~]# expr $num1 % $num2 2 # 兩個小括號的計算方法,要賦值,不然會報錯 [root@es01 ~]# $(($num1+$num2)) -bash: 22: command not found [root@es01 ~]# num3=$(($num1+$num2)) [root@es01 ~]# echo $num3 22 [root@es01 ~]# num3=$(($num1*$num2)) [root@es01 ~]# echo $num3 85 [root@es01 ~]# num3=$(($num1-$num2)) [root@es01 ~]# echo $num3 12 [root@es01 ~]# num3=$(($num1/$num2)) [root@es01 ~]# echo $num3 3 # 部分支持,= 不支持 [root@es01 ~]# num3=$(($num1>$num2)) [root@es01 ~]# echo $num3 1 [root@es01 ~]# num3=$(($num1<$num2)) [root@es01 ~]# echo $num3 0 [root@es01 ~]# num3=$(($num1=$num2)) -bash: 17=5: attempted assignment to non-variable (error token is "=5") 在比較運算的時候最好使用expr 練習例子: 提示用戶輸入一個正整數num,而後計算1+2+3+...+num的值;必須對num是否爲正整數作判斷,不符合應當運行再次輸入 # 判斷是否大於0 [root@es01 ~]# num1=56 [root@es01 ~]# expr $num1 \> 0 1 # expr只能對整數進行計算,直接用expr 和一個整數計算獲取 $? 的值來判斷是否爲整數 [root@es01 ~]# num1=56.58 [root@es01 ~]# expr $num1 \> 0 1 [root@es01 ~]# expr $num1 + 1 expr: non-integer argument [root@es01 ~]# echo $? 2 [root@es01 ~]# num1=90 [root@es01 ~]# expr $num1 + 1 91 [root@es01 ~]# echo $? 0 # 具體腳本 # vim sum.sh #!/bin/bash # while true do read -p "please input a positive number: " num # 判斷數是不是整數 expr $num + 1 &> /dev/null if [ $? -eq 0 ];then # 判斷這個整數是否大於0,大於0返回1 if [ `expr $num \> 0` -eq 1 ];then #echo "yes,positive number" # $sum沒有賦值,默認爲0 for((i=1;i<=$num;i++)) do sum=`expr $sum + $i` done echo "1+2+3+...+$num = $sum" # 執行計算須要退出 exit fi fi echo "error,input enlegal" continue done # bc介紹 bc是bash內建的運算器,支持浮點數運算 內建變量scale能夠設置,默認爲0 bc操做符對照表 操做符 含義 num1 + num2 求和 num1 - num2 求差 num1 * num2 求積 num1 / num2 求商 num1 % num2 求餘 num1 ^ num2 指數運算 centos7默認沒有安裝bc命令 yum install -y bc # 交互模式 [root@es01 shell]# 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'. 23 + 5 28 23 - 8 15 23 * 6 138 23 / 5 4 23 % 5 3 # 小數點保留2位 scale=2 23 / 5 4.60 # 小數點保留6位 scale=6 23 / 7 3.285714 # 腳本中使用管道符進行計算 [root@es01 shell]# echo "23+33" | bc 56 [root@es01 shell]# echo "23.3+66" | bc 89.3 # 保留精度 scale=4; 用分號隔開 [root@es01 shell]# echo "scale=4;23.3/3.5" | bc 6.6571 # bc示例腳本 [root@es01 shell]# cat bc.sh #!/bin/bash # read -p "num1: " num1 read -p "num2: " num2 #echo "scale=4;$num1/$num2" | bc num3=`echo "scale=4;$num1/$num2" | bc` echo "$num1 / $num2 = $num3" [root@es01 shell]# sh bc.sh num1: 5.6 num2: 3 5.6 / 3 = 1.8666