主頁(yè) > 知識(shí)庫(kù) > Shell腳本編程中常用的數(shù)學(xué)運(yùn)算實(shí)例

Shell腳本編程中常用的數(shù)學(xué)運(yùn)算實(shí)例

熱門標(biāo)簽:網(wǎng)站排名優(yōu)化 鐵路電話系統(tǒng) 地方門戶網(wǎng)站 百度競(jìng)價(jià)排名 呼叫中心市場(chǎng)需求 AI電銷 服務(wù)外包 Linux服務(wù)器

這部分主要討論數(shù)學(xué)相關(guān)的shell腳本編程。

加法運(yùn)算

新建一個(gè)文件“Addition.sh”,輸入下面的內(nèi)容并賦予其可執(zhí)行的權(quán)限。

復(fù)制代碼 代碼如下:
#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
x=$(expr "$a" + "$b")
echo $a + $b = $x

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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

減法運(yùn)算

復(fù)制代碼 代碼如下:

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

注意:這里我們沒有像上面的例子中使用“expr”來執(zhí)行數(shù)學(xué)運(yùn)算。

輸出結(jié)果:

復(fù)制代碼 代碼如下:

[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

乘法運(yùn)算

復(fù)制代碼 代碼如下:

#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
echo "$a * $b = $(expr $a \* $b)"

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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

除法運(yùn)算

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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

數(shù)組

下面的這個(gè)腳本可以打印一組數(shù)字。

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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

你可以從這里下載這個(gè)例子的代碼

判斷奇偶數(shù)

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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數(shù)

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:
[root@tecmint ~]# vi Factorial.sh
[root@tecmint ~]# chmod 755 Factorial.sh
[root@tecmint ~]# ./Factorial.sh
 
Enter The Number
12
479001600

你可以從這里下載這個(gè)例子的代碼

判斷Armstrong數(shù)

Armstrong數(shù):在三位的正整數(shù)中,例如abc,有一些可能滿足(a^3)+(b^3)+(c^3)=abc,即各個(gè)位數(shù)的立方和正好是該數(shù)的本身。這些數(shù)即稱為Armstrong數(shù)。

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[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

判斷質(zhì)數(shù)

復(fù)制代碼 代碼如下:

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

輸出結(jié)果:
復(fù)制代碼 代碼如下:

[root@tecmint ~]# vi Prime.sh
[root@tecmint ~]# chmod 755 Prime.sh
[root@tecmint ~]# ./Prime.sh
 
“Enter Any Number”
12
 
“Not Prime”

您可能感興趣的文章:
  • Shell(())實(shí)現(xiàn)對(duì)整數(shù)進(jìn)行數(shù)學(xué)運(yùn)算
  • Linux Shell腳本系列教程(五):數(shù)學(xué)運(yùn)算
  • Shell實(shí)現(xiàn)的一些數(shù)學(xué)運(yùn)算自定義函數(shù)分享
  • Windows Powershell 進(jìn)行數(shù)學(xué)運(yùn)算
  • 通過shell進(jìn)行數(shù)學(xué)運(yùn)算的多種方式
  • Shell中的數(shù)學(xué)運(yùn)算使用

標(biāo)簽:湘潭 蘭州 崇左 銅川 湖南 黃山 仙桃 衡水

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Shell腳本編程中常用的數(shù)學(xué)運(yùn)算實(shí)例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266