Shell腳本編程中常用的數(shù)學(xué)運(yùn)算實例
這部分主要討論數(shù)學(xué)相關(guān)的shell腳本編程。
加法運(yùn)算
新建一個文件“Addition.sh”,輸入下面的內(nèi)容并賦予其可執(zhí)行的權(quán)限。
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
x=$(expr "$a" + "$b")
echo $a + $b = $x
輸出結(jié)果:
[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)算
#!/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é)果:
[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)算
#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
echo "$a * $b = $(expr $a \* $b)"
輸出結(jié)果:
[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)算
#!/bin/bash
echo “Enter the First Number: ”
read a
echo “Enter the Second Number: ”
read b
echo "$a / $b = $(expr $a / $b)"
輸出結(jié)果:
[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ù)組
下面的這個腳本可以打印一組數(shù)字。
#!/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é)果:
[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ù)
#!/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é)果:
[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ù)
#!/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é)果:
[root@tecmint ~]# chmod 755 Factorial.sh
[root@tecmint ~]# ./Factorial.sh
Enter The Number
12
479001600
你可以從這里下載這個例子的代碼
判斷Armstrong數(shù)
Armstrong數(shù):在三位的正整數(shù)中,例如abc,有一些可能滿足(a^3)+(b^3)+(c^3)=abc,即各個位數(shù)的立方和正好是該數(shù)的本身。這些數(shù)即稱為Armstrong數(shù)。
#!/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é)果:
[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ù)
#!/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é)果:
[root@tecmint ~]# vi Prime.sh
[root@tecmint ~]# chmod 755 Prime.sh
[root@tecmint ~]# ./Prime.sh
“Enter Any Number”
12
“Not Prime”
- Linux命令行和shell腳本編程寶典 Richard Blum
- shell腳本編程之for語句、if語句使用介紹
- Linux shell腳本編程if語句的使用方法(條件判斷)
- 5個Shell腳本編程入門練習(xí)例子
- shell 編程中空格的使用方法
- Shell 編程:Bash空格的那點事
- shell腳本編程實現(xiàn)9*9乘法表
- Linux Shell 腳本編程入門教程
- Shell腳本編程之判斷語句
- shell腳本編程之循環(huán)語句
- Shell編程中while與for的區(qū)別及用法詳解
- Linux下Shell編程快捷鍵大全(日常整理)
- 詳解Shell編程之變量數(shù)值計算(一)
- 詳解Shell編程之if語句實戰(zhàn)(小結(jié))
- Shell編程之case語句實戰(zhàn)(小結(jié))
- 對Linux下shell編程之for循環(huán)的實例講解
- Shell腳本編程30分鐘入門(小結(jié))
- shell編程入門之變量,字符串,數(shù)組,函數(shù)使用方法總結(jié)
相關(guān)文章
一天一個shell命令 linux文本操作系列-tree命令詳解
這篇文章主要介紹了一天一個shell命令 linux文本操作系列-tree命令詳解,需要的朋友可以參考下2016-06-06
ubuntu編譯pyav報錯libx264?not?found解決示例
這篇文章主要為大家介紹了ubuntu編譯pyav報錯libx264?not?found解決示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
Shell中的循環(huán)語句for、while、until實例講解
這篇文章主要介紹了Shell中的循環(huán)語句for、while、until實例講解,簡單清晰明了,非常不錯的教程,需要的朋友可以參考下2014-06-06

