国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > 幫助中心 > 正文

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

2024-07-09 22:41:43
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

這部分主要討論數(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”來(lái)執(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ā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 石楼县| 邢台市| 上杭县| 峨眉山市| 姜堰市| 崇礼县| 怀安县| 松滋市| 伊宁市| 亚东县| 南开区| 盘山县| 板桥市| 通化县| 南漳县| 盐源县| 伊川县| 三台县| 永兴县| 丹江口市| 天峻县| 淳化县| 营口市| 永城市| 密山市| 卫辉市| 新化县| 浮山县| 蓬莱市| 横山县| 桃园县| 明溪县| 门头沟区| 通州区| 河东区| 仪征市| 社旗县| 大安市| 天柱县| 宝兴县| 金山区|