Shell脚本编程中常用的数学运算实例
这部分主要讨论数学相关的shell脚本编程。
加法运算
新建一个文件“Addition.sh”,输入下面的内容并赋予其可执行的权限。
#!/bin/bash echo“EntertheFirstNumber:” reada echo“EntertheSecondNumber:” readb x=$(expr"$a"+"$b") echo$a+$b=$x
输出结果:
[root@tecmint~]#viAdditions.sh [root@tecmint~]#chmod755Additions.sh [root@tecmint~]#./Additions.sh “EntertheFirstNumber:” 12 “EntertheSecondNumber:” 13 12+13=25
减法运算
#!/bin/bash echo“EntertheFirstNumber:” reada echo“EntertheSecondNumber:” readb x=$(($a-$b)) echo$a-$b=$x
注意:这里我们没有像上面的例子中使用“expr”来执行数学运算。
输出结果:
[root@tecmint~]#viSubstraction.sh [root@tecmint~]#chmod755Substraction.sh [root@tecmint~]#./Substraction.sh “EntertheFirstNumber:” 13 “EntertheSecondNumber:” 20 13-20=-7
乘法运算
#!/bin/bash echo“EntertheFirstNumber:” reada echo“EntertheSecondNumber:” readb echo"$a*$b=$(expr$a\*$b)"
输出结果:
[root@tecmint~]#viMultiplication.sh [root@tecmint~]#chmod755Multiplication.sh [root@tecmint~]#./Multiplication.sh “EntertheFirstNumber:” 11 “EntertheSecondNumber:” 11 11*11=12
除法运算
#!/bin/bash echo“EntertheFirstNumber:” reada echo“EntertheSecondNumber:” readb echo"$a/$b=$(expr$a/$b)"
输出结果:
[root@tecmint~]#viDivision.sh [root@tecmint~]#chmod755Division.sh [root@tecmint~]#./Division.sh “EntertheFirstNumber:” 12 “EntertheSecondNumber:” 3 12/3=4
数组
下面的这个脚本可以打印一组数字。
#!/bin/bash echo“EnterTheNumberuptowhichyouwanttoPrintTable:” readn i=1 while[$i-ne10] do i=$(expr$i+1) table=$(expr$i\*$n) echo$table done
输出结果:
[root@tecmint~]#viTable.sh [root@tecmint~]#chmod755Table.sh [root@tecmint~]#./Table.sh “EnterTheNumberuptowhichyouwanttoPrintTable:” 29 58 87 116 145 174 203 232 261 290
你可以从这里下载这个例子的代码
判断奇偶数
#!/bin/bash echo"EnterTheNumber" readn num=$(expr$n%2) if[$num-eq0] then echo"isaEvenNumber" else echo"isaOddNumber" fi
输出结果:
[root@tecmint~]#viEvenOdd.sh [root@tecmint~]#chmod755EvenOdd.sh [root@tecmint~]#./EvenOdd.sh EnterTheNumber 12 isaEvenNumber 1 2 3 4 5 [root@tecmint~]#./EvenOdd.sh EnterTheNumber 11 isaOddNumber
Factorial数
#!/bin/bash echo"EnterTheNumber" reada fact=1 while[$a-ne0] do fact=$(expr$fact\*$a) a=$(expr$a-1) done echo$fact
输出结果:
[root@tecmint~]#viFactorial.sh [root@tecmint~]#chmod755Factorial.sh [root@tecmint~]#./Factorial.sh EnterTheNumber 12 479001600
你可以从这里下载这个例子的代码
判断Armstrong数
Armstrong数:在三位的正整数中,例如abc,有一些可能满足(a^3)+(b^3)+(c^3)=abc,即各个位数的立方和正好是该数的本身。这些数即称为Armstrong数。
#!/bin/bash echo"EnterANumber" readn arm=0 temp=$n while[$n-ne0] do r=$(expr$n%10) arm=$(expr$arm+$r\*$r\*$r) n=$(expr$n/10) done echo$arm if[$arm-eq$temp] then echo"Armstrong" else echo"NotArmstrong" fi
输出结果:
[root@tecmint~]#viArmstrong.sh [root@tecmint~]#chmod755Armstrong.sh [root@tecmint~]#./Armstrong.sh EnterANumber 371 371 Armstrong 1 2 3 4 5 6 [root@tecmint~]#./Armstrong.sh EnterANumber 123 36 NotArmstrong
判断质数
#!/bin/bash echo“EnterAnyNumber” readn i=1 c=1 while[$i-le$n] do i=$(expr$i+1) r=$(expr$n%$i) if[$r-eq0] then c=$(expr$c+1) fi done if[$c-eq2] then echo“Prime” else echo“NotPrime” fi
输出结果:
[root@tecmint~]#viPrime.sh [root@tecmint~]#chmod755Prime.sh [root@tecmint~]#./Prime.sh “EnterAnyNumber” 12 “NotPrime”