ลักษณะทางคณิตศาสตร์ของการเขียนโปรแกรม Linux Shell - ตอนที่ 4
ในโพสต์นี้ ฉันจะพูดถึง Sสคริปต์ จากมุมมอง คณิตศาสตร์ และ ตัวเลข แม้ว่าฉันจะโพสต์สคริปต์ที่ซับซ้อนกว่านี้ (Simple Calculator) ในโพสต์ที่แล้ว แต่ในส่วนของผู้ใช้นั้นเป็นเรื่องยากที่จะเข้าใจ และด้วยเหตุนี้ ฉันจึงคิดที่จะทำให้ผู้คนได้เรียนรู้อีกด้านที่เป็นประโยชน์ของการเรียนรู้ใน แพ็คเก็ตขนาดเล็ก
ก่อนบทความนี้ มีการเผยแพร่บทความ Shell Scripting Series สามบทความ ได้แก่:
- ทำความเข้าใจกับ Linux Shell และ Basic Shell Scripting – ตอนที่ 1
- 5 เชลล์สคริปต์เพื่อเรียนรู้การเขียนโปรแกรมเชลล์ – ตอนที่ II
- ล่องเรือผ่านโลกแห่งการเขียนสคริปต์ Linux BASH - ตอนที่ III
มาเริ่มต้นกระบวนการเรียนรู้เพิ่มเติมด้วยสคริปต์ใหม่ๆ ที่น่าตื่นเต้น เริ่มต้นด้วยสคริปต์ คณิตศาสตร์:
สคริปต์ 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 และปล่อยให้การคำนวณทางคณิตศาสตร์ดำเนินการในเชลล์
ผลลัพธ์ตัวอย่าง
[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
ดาวน์โหลด Substraction.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
ดาวน์โหลด Multiplication.sh
สคริปต์ 4: กอง
ขวา! ถัดไปคือ แผนก และอีกครั้งเป็นสคริปต์ที่เรียบง่ายมาก ตรวจสอบด้วยตัวเอง
#!/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
ดาวน์โหลด Table.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! หรืออะไรทำนองนั้น แน่นอน! สคริปต์นี้จะมีประโยชน์มากในสถานการณ์นั้น
ดาวน์โหลด Factorial.sh
สคริปต์ที่ 8: อาร์มสตรอง
เบอร์อาร์มสตรอง! โอ้ คุณลืมไปแล้วว่า Armstrong Number คืออะไร จำนวนสามหลักของอาร์มสตรองเป็นจำนวนเต็มซึ่งผลรวมของลูกบาศก์ของหลักจะเท่ากับตัวเลขนั้นเอง ตัวอย่างเช่น 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
ดาวน์โหลด Armstrong.sh
สคริปต์ 9: นายกรัฐมนตรี
สคริปต์สุดท้ายคือการแยกแยะว่าตัวเลขนั้นเป็นจำนวนเฉพาะหรือไม่
#!/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
นั่นคือทั้งหมดที่สำหรับตอนนี้. ในบทความถัดไป เราจะกล่าวถึงโปรแกรมทางคณิตศาสตร์อื่นๆ ในภาษาการเขียนโปรแกรมเชลล์สคริปต์ อย่าลืมพูดถึงมุมมองของคุณเกี่ยวกับบทความในส่วนความคิดเห็น กดไลค์และแชร์เราและช่วยเราเผยแพร่ เยี่ยมชม linux-console.net เพื่อดู ข่าวสาร และบทความที่เกี่ยวข้องกับ FOSS ถึงตอนนั้นคอยติดตาม