从新手到系统管理员(四):Linux Shell脚本编程之数学(Part I)

本文由 [茶话汇] – [Qing] 编译自 [Avishek Kumar] 转载请注明出处

Shell-Scripting-Part-41

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

加法运算

新建一个文件“Addition.sh”,输入下面的内容并赋予其可执行的权限。

#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
x=<span class="katex math inline">(expr "</span>a" + "<span class="katex math inline">b")
echo</span>a + <span class="katex math inline">b =</span>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=<span class="katex math inline">((</span>a – <span class="katex math inline">b))
echo</span>a – <span class="katex math inline">b =</span>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 "<span class="katex math inline">a *</span>b = <span class="katex math inline">(expr</span>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 "<span class="katex math inline">a /</span>b = <span class="katex math inline">(expr</span>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 [ <span class="katex math inline">i -ne 10 ]
do
i=</span>(expr <span class="katex math inline">i + 1)
table=</span>(expr <span class="katex math inline">i /*</span>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=<span class="katex math inline">(expr</span>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
[root@tecmint ~]# ./EvenOdd.sh

Enter The Number
11
is a Odd Number

你可以从这里下载这个例子的代码

Factorial数

#!/bin/bash
echo "Enter The Number"
read a
fact=1
while [ <span class="katex math inline">a -ne 0 ]
do
fact=</span>(expr <span class="katex math inline">fact /*</span>a)
a=<span class="katex math inline">(expr</span>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=<span class="katex math inline">n
while [</span>n -ne 0 ]
do
r=<span class="katex math inline">(expr</span>n % 10)
arm=<span class="katex math inline">(expr</span>arm + <span class="katex math inline">r /*</span>r /* <span class="katex math inline">r)
n=</span>(expr <span class="katex math inline">n / 10)
done
echo</span>arm
if [ <span class="katex math inline">arm -eq</span>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
[root@tecmint ~]# ./Armstrong.sh

Enter A Number
123
36
Not Armstrong

你可以从这里下载这个例子的代码

判断质数

#!/bin/bash
echo “Enter Any Number”
read n
i=1
c=1
while [ <span class="katex math inline">i -le</span>n ]
do
i=<span class="katex math inline">(expr</span>i + 1)
r=<span class="katex math inline">(expr</span>n % <span class="katex math inline">i)
if [</span>r -eq 0 ]
then
c=<span class="katex math inline">(expr</span>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”

你可以从这里下载这个例子的代码

本文链接:http://www.yunweipai.com/3652.html

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/53213.html

(0)
上一篇 2021年8月6日 18:13
下一篇 2021年8月6日 18:13

相关推荐

发表回复

登录后才能评论