Shell脚本编程中常用的数学运算实例
2014-06-21 00:00
1041 查看
这部分主要讨论数学相关的shell脚本编程。
加法运算
新建一个文件“Addition.sh”,输入下面的内容并赋予其可执行的权限。
输出结果:
减法运算
注意:这里我们没有像上面的例子中使用“expr”来执行数学运算。
输出结果:
乘法运算
输出结果:
除法运算
输出结果:
数组
下面的这个脚本可以打印一组数字。
输出结果:
你可以从这里下载这个例子的代码
判断奇偶数
输出结果:
Factorial数
输出结果:
你可以从这里下载这个例子的代码
判断Armstrong数
Armstrong数:在三位的正整数中,例如abc,有一些可能满足(a^3)+(b^3)+(c^3)=abc,即各个位数的立方和正好是该数的本身。这些数即称为Armstrong数。
输出结果:
判断质数
输出结果:
加法运算
新建一个文件“Addition.sh”,输入下面的内容并赋予其可执行的权限。
#!/bin/bash echo “Enter the First Number: ” read a echo “Enter the Second Number: ” read b x=$(expr "$a" + "$b") echo $a + $b = $x
输出结果:
[root@tecmint ~]# vi Additions.sh [root@tecmint ~]# chmod 755 Additions.sh [root@tecmint ~]# ./Additions.sh “Enter the First Number: ” 12 “Enter the Second Number: ” 13 12 + 13 = 25
减法运算
#!/bin/bash echo “Enter the First Number: ” read a echo “Enter the Second Number: ” read b x=$(($a - $b)) echo $a - $b = $x
注意:这里我们没有像上面的例子中使用“expr”来执行数学运算。
输出结果:
[root@tecmint ~]# vi Substraction.sh [root@tecmint ~]# chmod 755 Substraction.sh [root@tecmint ~]# ./Substraction.sh “Enter the First Number: ” 13 “Enter the Second Number: ” 20 13 - 20 = -7
乘法运算
#!/bin/bash echo “Enter the First Number: ” read a echo “Enter the Second Number: ” read b echo "$a * $b = $(expr $a \* $b)"
输出结果:
[root@tecmint ~]# vi Multiplication.sh [root@tecmint ~]# chmod 755 Multiplication.sh [root@tecmint ~]# ./Multiplication.sh “Enter the First Number: ” 11 “Enter the Second Number: ” 11 11 * 11 = 12
除法运算
#!/bin/bash echo “Enter the First Number: ” read a echo “Enter the Second Number: ” read b echo "$a / $b = $(expr $a / $b)"
输出结果:
[root@tecmint ~]# vi Division.sh [root@tecmint ~]# chmod 755 Division.sh [root@tecmint ~]# ./Division.sh “Enter the First Number: ” 12 “Enter the Second Number: ” 3 12 / 3 = 4
数组
下面的这个脚本可以打印一组数字。
#!/bin/bash echo “Enter The Number upto which you want to Print Table: ” read n i=1 while [ $i -ne 10 ] do i=$(expr $i + 1) table=$(expr $i \* $n) echo $table done
输出结果:
[root@tecmint ~]# vi Table.sh [root@tecmint ~]# chmod 755 Table.sh [root@tecmint ~]# ./Table.sh “Enter The Number upto which you want to Print Table: ” 29 58 87 116 145 174 203 232 261 290
你可以从这里下载这个例子的代码
判断奇偶数
#!/bin/bash echo "Enter The Number" read n num=$(expr $n % 2) if [ $num -eq 0 ] then echo "is a Even Number" else echo "is a Odd Number" fi
输出结果:
[root@tecmint ~]# vi EvenOdd.sh [root@tecmint ~]# chmod 755 EvenOdd.sh [root@tecmint ~]# ./EvenOdd.sh Enter The Number 12 is a Even Number 1 2 3 4 5 [root@tecmint ~]# ./EvenOdd.sh Enter The Number 11 is a Odd Number
Factorial数
#!/bin/bash echo "Enter The Number" read a fact=1 while [ $a -ne 0 ] do fact=$(expr $fact \* $a) a=$(expr $a - 1) done echo $fact
输出结果:
[root@tecmint ~]# vi Factorial.sh [root@tecmint ~]# chmod 755 Factorial.sh [root@tecmint ~]# ./Factorial.sh Enter The Number 12 479001600
你可以从这里下载这个例子的代码
判断Armstrong数
Armstrong数:在三位的正整数中,例如abc,有一些可能满足(a^3)+(b^3)+(c^3)=abc,即各个位数的立方和正好是该数的本身。这些数即称为Armstrong数。
#!/bin/bash echo "Enter A Number" read n arm=0 temp=$n while [ $n -ne 0 ] 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 "Not Armstrong" fi
输出结果:
[root@tecmint ~]# vi Armstrong.sh [root@tecmint ~]# chmod 755 Armstrong.sh [root@tecmint ~]# ./Armstrong.sh Enter A Number 371 371 Armstrong 1 2 3 4 5 6 [root@tecmint ~]# ./Armstrong.sh Enter A Number 123 36 Not Armstrong
判断质数
#!/bin/bash echo “Enter Any Number” read n i=1 c=1 while [ $i -le $n ] do i=$(expr $i + 1) r=$(expr $n % $i) if [ $r -eq 0 ] then c=$(expr $c + 1) fi done if [ $c -eq 2 ] then echo “Prime” else echo “Not Prime” fi
输出结果:
[root@tecmint ~]# vi Prime.sh [root@tecmint ~]# chmod 755 Prime.sh [root@tecmint ~]# ./Prime.sh “Enter Any Number” 12 “Not Prime”
相关文章推荐
- Shell脚本编程中常用的数学运算实例
- Shell脚本编程中常用的数学运算实例
- shell编程实例---实现整数的加减乘除余运算(利用read和脚本传参的方式)
- shell常用编程及实例
- Shell脚本编程——查找和过滤的常用命令
- CentOS6、7 LVM逻辑卷分区自动扩容Shell脚本编程思路与实例
- Shell 脚本中的算术运算与常用条件表达式
- Shell 编程--本文结合大量实例阐述如何编写一个shell脚本
- shell脚本编程简单实例
- Shell 编程--本文结合大量实例阐述如何编写一个shell脚本
- bash脚本编程之条件判断、算术运算及实例
- Shell 编程--本文结合大量实例阐述如何编写一个shell脚本
- shell编程之数学运算
- *Linux Shell 高级编程技巧4----几个常用的shell脚本例子
- Shell 编程--本文结合大量实例阐述如何编写一个shell脚本。
- LinuxC/C++编程基础(13) shell脚本简单实例
- 查看: 35330 | 回复: 9 打印 上一主题 下一主题 [学习共享] 转:Shell 编程--本文结合大量实例阐述如何编写一个shell脚本
- Linux常用命令全集 linux_Shell(脚本)编程入门_实例讲解详解
- CentOS6、7 LVM逻辑卷分区自动扩容Shell脚本编程思路与实例
- Shell脚本处理浮点数的运算和比较实例