网站搜索

Linux Shell 编程的数学方面 - 第四部分


在这篇文章中,我将从数学数字的角度讨论S脚本。虽然我在上一篇文章中发布了一个更复杂的脚本(简单计算器),但在用户部分很难理解,因此我想让你们了解学习的其他有用的方面小包。

在本文之前,已经发表过三篇Shell脚本系列文章,分别是:

  1. 了解 Linux Shell 和基本 Shell 脚本编写 – 第一部分
  2. 学习 Shell 编程的 5 个 Shell 脚本 – 第二部分
  3. 畅游 Linux BASH 脚本世界 – 第三部分

让我们从一些新的令人兴奋的脚本开始进一步的学习过程,从数学脚本开始:

脚本 1:添加

按照上一篇文章中的描述,为脚本创建一个文件“Addition.sh”和 chmod 755 并运行它。

#!/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

下载Additions.sh

脚本 2:减法

#!/bin/bash
echo “Enter the First Number: ” 
read a 
echo “Enter the Second Number: ” 
read b 
x=$(($a - $b)) 
echo $a - $b = $x

注意:这里我们替换了expr,让数学计算在shell中进行。

样本输出
[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

下载减法.sh

脚本 3:乘法

到目前为止,您会很享受,以如此简单的方式学习脚本,所以按时间顺序的下一个是乘法

#!/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 = 121

下载乘法.sh

脚本 4:除法

正确的!接下来是Division,这又是一个非常简单的脚本。你自己检查一下。

#!/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

下载Division.sh

脚本 5:表格

美好的!经过这些基本的数学运算之后会发生什么。让我们编写一个打印任意数字的表格的脚本。

#!/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

下载表.sh

脚本 6:偶数

我们小时候总是计算数字是奇数还是偶数。用脚本来实现不是一个好主意吗?

#!/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
[root@tecmint ~]# ./EvenOdd.sh

Enter The Number 
11 
is a Odd Number

下载EvenOdd.sh

脚本 7:阶乘

接下来是求阶乘。

#!/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

您现在可能会放松地感觉计算 12*11*10*9*7*7*6*5*4*3*2*1 比上面生成的简单脚本更困难。想象一下您需要找到 99! 或类似内容的情况。当然!在这种情况下这个脚本会非常方便。

下载阶乘.sh

剧本8:阿姆斯特朗

阿姆斯特朗数字!哦哦,你忘了什么是阿姆斯特朗数。三位数的阿姆斯特朗数是一个整数,其数字的立方之和等于该数本身。例如,371 是一个阿姆斯壮数,因为3**3 + 7**3 + 1**3=371

#!/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
[root@tecmint ~]# ./Armstrong.sh

Enter A Number 
123 
36 
Not Armstrong

下载阿姆斯特朗.sh

脚本9:Prime

最后一个脚本是区分一个数字是否是素数。

#!/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”

下载Prime.sh

目前为止就这样了。在我们的下一篇文章中,我们将介绍 shell 脚本编程语言中的其他数学程序。不要忘记在评论部分提及您对文章的看法。喜欢并分享我们并帮助我们传播。欢迎访问 howtoing.com,获取与 FOSS 相关的新闻和文章。到那时请继续关注。