Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

Deep Dive: แนวคิดคณิตศาสตร์ใน Linux Shell Scripting – ตอนที่ 4

ในโพสต์นี้ผมจะอภิปราย Scripts จากมุมมอง Mathematical และ Number แม้ผมจะโพสต์สคริปต์ที่ซับซ้อนกว่า (Simple Calculator) ในโพสต์ก่อนหน้า แต่ในมุมมองของผู้ใช้ ยากที่จะเข้าใจ ดังนั้นผมคิดว่าควรให้คุณคนๆ เรียนรู้ด้านที่มีประโยชน์อีกด้านหนึ่งในแพ็กเกจเล็กๆ

ก่อนหน้านี้ บทความ Shell Scripting Series 3 บทได้รับการเผยแพร่แล้ว และคือ:

  1. Understand Linux Shell and Basic Shell Scripting – Part I
  2. 5 Shell Scripts to Learn Shell Programming – Part II
  3. Sailing Through The World of Linux BASH Scripting – Part III

เริ่มกระบวนการเรียนรู้ต่อไปกับสคริปต์ใหม่ที่น่าตื่นเต้น เริ่มต้นด้วยสคริปต์ Mathematics:

สคริปต์ที่ 1: การบวก (Additions)

สร้างไฟล์ "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
ตัวอย่างผลลัพธ์
[admin@wsxdn.com ~]# vi Additions.sh
[admin@wsxdn.com ~]# chmod 755 Additions.sh
[admin@wsxdn.com ~]# ./Additions.sh
"Enter the First Number: " 12
"Enter the Second Number: " 13
12 + 13 = 25

ดาวน์โหลด Additions.sh

สคริปต์ที่ 2: การลบ (Subtraction)

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

หมายเหตุ: ที่นี่เราแทนที่ expr และให้การคำนวณคณิตศาสตร์ทำงานในเชลล์แทน

ตัวอย่างผลลัพธ์
[admin@wsxdn.com ~]# vi Substraction.sh
[admin@wsxdn.com ~]# chmod 755 Substraction.sh
[admin@wsxdn.com ~]# ./Substraction.sh
"Enter the First Number: " 13
"Enter the Second Number: " 20
13 - 20 = -7

ดาวน์โหลด Substraction.sh

สคริปต์ที่ 3: การคูณ (Multiplication)

จนถึงที่นี่คุณคงเพลินเพลินกับการเรียนรู้สคริปต์อย่างง่ายดาย ตัวถัดไปในลำดับคือ Multiplication

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

หมายเหตุ: ใช่! ที่นี่เราไม่ได้เก็บค่าการคูณในตัวแปร แต่ดำเนินการโดยตรงในคำสั่งแสดงผล

ตัวอย่างผลลัพธ์
[admin@wsxdn.com ~]# vi Multiplication.sh
[admin@wsxdn.com ~]# chmod 755 Multiplication.sh
[admin@wsxdn.com ~]# ./Multiplication.sh
"Enter the First Number: " 11
"Enter the Second Number: " 11
11 * 11 = 121

ดาวน์โหลด Multiplication.sh

สคริปต์ที่ 4: การหาร (Division)

ถูกต้อง! ตัวถัดไปคือ Division และอีกครั้งเป็นสคริปต์ง่ายมาก ลองตรวจสอบด้วยตัวเอง

#!/bin/bash
echo "Enter the First Number: "
read a
echo "Enter the Second Number: "
read b
echo "$a / $b = $(expr $a / $b)"
ตัวอย่างผลลัพธ์
[admin@wsxdn.com ~]# vi Division.sh
[admin@wsxdn.com ~]# chmod 755 Division.sh
[admin@wsxdn.com ~]# ./Division.sh
"Enter the First Number: " 12
"Enter the Second Number: " 3
12 / 3 = 4

ดาวน์โหลด Division.sh

สคริปต์ที่ 5: ตารางคูณ (Table)

ดีแล้ว! หลังจากการดำเนินการคณิตศาสตร์พื้นฐานเหล่านี้ มาเขียนสคริปต์ที่พิมพ์ตารางคูณของตัวเลขใดๆ

#!/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
ตัวอย่างผลลัพธ์
[admin@wsxdn.com ~]# vi Table.sh
[admin@wsxdn.com ~]# chmod 755 Table.sh
[admin@wsxdn.com ~]# ./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: คี่หรือคู่ (EvenOdd)

เราเมื่อเป็นเด็กก็เคยคำนวณหาว่าตัวเลขเป็นคี่หรือคู่ ไม่ใช่ไหมดีที่จะนำไปปฏิบัติในสคริปต์

#!/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
ตัวอย่างผลลัพธ์
[admin@wsxdn.com ~]# vi EvenOdd.sh
[admin@wsxdn.com ~]# chmod 755 EvenOdd.sh
[admin@wsxdn.com ~]# ./EvenOdd.sh
Enter The Number
12
is a Even Number
[admin@wsxdn.com ~]# ./EvenOdd.sh
Enter The Number
11
is a Odd Number

ดาวน์โหลด EvenOdd.sh

สคริปต์ที่ 7: แฟกทอเรียล (Factorial)

ต่อไปคือการหาแฟกทอเรียล

#!/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
ตัวอย่างผลลัพธ์
[admin@wsxdn.com ~]# vi Factorial.sh
[admin@wsxdn.com ~]# chmod 755 Factorial.sh
[admin@wsxdn.com ~]# ./Factorial.sh
Enter The Number
12
479001600

คุณอาจผ่อนคลายกับความรู้สึกว่าการคำนวณ 12*11*10*9*7*7*6*5*4*3*2*1 จะยากกว่าสคริปต์ง่ายๆ ที่สร้างขึ้นข้างต้น มากกว่านั้น คิดถึงสถานการณ์ที่คุณต้องการหา 99! หรืออย่างนั้น แน่นอน! สคริปต์นี้จะช่วยได้มากในสถานการณ์นั้น

ดาวน์โหลด Factorial.sh

สคริปต์ที่ 8: อาร์มสตรอง (Armstrong)

Armstrong Number! โอ้ คุณลืมว่า Armstrong Number คืออะไร ไม่เป็นไร ตัวเลขอาร์มสตรอง 3 หลัก คือจำนวนเต็มที่ผลรวมของกำลังสามของหลักต่างๆ เท่ากับตัวเลขนั้นเอง เช่น 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
ตัวอย่างผลลัพธ์
[admin@wsxdn.com ~]# vi Armstrong.sh
[admin@wsxdn.com ~]# chmod 755 Armstrong.sh
[admin@wsxdn.com ~]# ./Armstrong.sh
Enter A Number
371
371
Armstrong
[admin@wsxdn.com ~]# ./Armstrong.sh
Enter A Number
123
36
Not Armstrong

ดาวน์โหลด 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
ตัวอย่างผลลัพธ์
[admin@wsxdn.com ~]# vi Prime.sh
[admin@wsxdn.com ~]# chmod 755 Prime.sh
[admin@wsxdn.com ~]# ./Prime.sh
"Enter Any Number" 12
"Not Prime"

ดาวน์โหลด Prime.sh

ขอจบเพียงเท่านี้สำหรับตอนนี้ ในบทความหน้าของเรา เราจะครอบคลุมโปรแกรมคณิตศาสตร์อื่นๆ ในภาษา shell Scripting programming อย่าลืมระบุมุมมองของคุณเกี่ยวกับบทความในส่วนความคิดเห็น Like และ share เรา และช่วยเราเผยแพร่ มาเยี่ยม tecmint.com สำหรับ News และบทความเกี่ยวกับ FOSS จนกว่าจะพบกันใหม่ Stay tuned