Shell脚本编程中常用的数学运算实例

所属分类: 脚本专栏 / linux shell 阅读数: 936
收藏 0 赞 0 分享

这部分主要讨论数学相关的shell脚本编程。

加法运算

新建一个文件“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”

更多精彩内容其他人还在看

Apache使用 .htaccess 来实现强制https访问的方法

下面小编就为大家带来一篇Apache使用 .htaccess 来实现强制https访问的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Linux shell 之 提取文件名和目录名的一些方法总结

本篇文章主要介绍了Linux shell 之 提取文件名和目录名的一些方法总结,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享

详解shell 遍历文件夹内所有文件并打印绝对路径

本篇文章主要介绍了shell 遍历文件夹内所有文件并打印绝对路径,具有一定的参考价值,有兴趣的可以了解一下。
收藏 0 赞 0 分享

在linux的终端退出python命令行的方法

下面小编就为大家带来一篇在linux的终端退出python命令行的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Log4j 日志文件Linux/Mac/Windows通用存放位置设置方法

下面小编就为大家带来一篇Log4j 日志文件Linux/Mac/Windows通用存放位置设置方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Linux shell命令帮助格式详解

最近看了一个教程,关于Linux命令的,本来以为当是复习随便看看的,结果看了不禁汗颜,这个真挺有学问的,很多东西都是我还不知道的,故此做总结。下面这篇文章主要介绍了Linux shell命令帮助格式的相关资料,需要的朋友可以参考借鉴。
收藏 0 赞 0 分享

linux shell发送Email邮件的方法详解

这篇文章主要介绍了linux shell发送Email邮件的方法,文中介绍的内容包括发送一封简单的邮件、邮件的格式、邮件标题使用中文以及邮件内容使用html等相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。
收藏 0 赞 0 分享

详解Linux命令中的正则表达式

正则表达式是一套由多个元字符组成的模糊查找模式,使用正则表达式可以快速查找和定位文本中指定的内容。接下来通过本文给大家介绍Linux命令中的正则表达式,需要的朋友参考下吧
收藏 0 赞 0 分享

linux下安装配置Memcache和PHP环境的实现

下面小编就为大家带来一篇linux下安装配置Memcache和PHP环境的实现。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

php在linux中可能用到的命令(推荐)

下面小编就为大家带来一篇php在linux中可能用到的命令(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多