Bash Conditional Statements ทำ Computations หรือ Actions ต่างๆ ขึ้นอยู่กับว่า Programmer-specified Boolean Condition Evaluate เป็น True หรือ False Statements นี้ใช้ Execute ส่วนต่างๆ ของ Shell Program ขึ้นอยู่กับว่า Conditions เหล่านั้น True หรือไม่ ความสามารถในการ Branch ทำให้ Shell Scripts อันทรงพลัง
ใน Bash มี Conditional Statements ดังนี้:
- if..then..fi Statement (Simple If)
- if..then..else..fi Statement (If-Else)
- if..elif..else..fi Statement (Else If Ladder)
- if..then..else..if..then..fi..fi.. (Nested If)
เหล่านี้คล้าย Awk If Statements ที่พูดไปก่อนหน้า
1. Bash If..then..fi Statement
if [ conditional expression ] then statement1 statement2 . fi
If Statement นี้เรียกว่า Simple If Statement ถ้า Conditional Expression ที่ให้มาเป็น True จะเข้าและ Execute Statements ระหว่าง Keywords "then" และ "fi" ถ้า Expression คืนค่าศูนย์ Consequent Statement List จะ Execute
If Then Fi Example:
#!/bin/bash count=100 if [ $count -eq 100 ] then echo "Count is 100" fi
2. Bash If..then..else..fi Statement
If [ conditional expression ] then statement1 statement2 . else statement3 statement4 . fi
ถ้า Conditional Expression เป็น True Execute Statement 1 และ 2 ถ้า Conditional Expression คืนค่าศูนย์ จะ Jump ไป Else Part และ Execute Statement 3 และ 4 หลัง Execution ของ If/Else Part Execution Resume กับ Consequent Statements
If Then Else Fi Example:
#!/bin/bash count=99 if [ $count -eq 100 ] then echo "Count is 100" else echo "Count is not 100" fi
หมายเหตุ: บทความนี้เป็นส่วนหนึ่งของชุด Bash Tutorial Series ต่อเนื่อง
3. Bash If..elif..else..fi
If [ conditional expression1 ] then statement1 statement2 . elif [ conditional expression2 ] then statement3 statement4 . . else statement5 fi
ใช้ If .. Elif.. If นี้ ถ้าต้องการ Select หนึ่งในหลาย Blocks of Code เพื่อ Execute มัน Check Expression 1 ถ้า True Execute Statement 1,2 ถ้า Expression1 False Check Expression2 และถ้า All Expressions False จะเข้า Else Block Execute Statements ใน Else Block
If Then Elif Then Else Fi Example:
#!/bin/bash count=99 if [ $count -eq 100 ] then echo "Count is 100" elif [ $count -gt 100 ] then echo "Count is greater than 100" else echo "Count is less than 100" fi
4. Bash If..then..else..if..then..fi..fi.. (Nested If)
If [ conditional expression1 ]
then
statement1
statement2
.
else
if [ conditional expression2 ]
then
statement3
.
fi
fi
If Statement และ Else Statement สามารถ Nested ได้ใน Bash Keyword "fi" บ่งชี้ End ของ Inner If Statement และ If Statements ทั้งหมดควบจบด้วย Keyword "fi"
"if then elif then else fi" Example ที่กล่าวมาข้างต้น สามารถ Convert เป็น Nested If ได้ดังนี้
#!/bin/bash
count=99
if [ $count -eq 100 ]
then
echo "Count is 100"
else
if [ $count -gt 100 ]
then
echo "Count is greater than 100"
else
echo "Count is less than 100"
fi
fi
ในบทความถัดไป จะพูดถึงการใช้ Bash Conditional Expressions พร้อมตัวอย่างปฏิบัติ
Recommended Reading
Bash 101 Hacks, by Ramesh Natarajan ผมใช้เวลาส่วนใหญ่กับ Linux Environment จึงเป็นแฟนใหญ่ของ Bash Command Line และ Shell Scripting 15 ปีก่อน ทำงานบน *nix หลายรสชาติ เคยเขียน Code มากบน C Shell และ Korn Shell ต่อมาเริ่มทำงานบน Linux เป็น System Administrator Automate ทุกอย่างที่เป็นไปได้ด้วย Bash Shell Scripting จากประสบการณ์ Bash เขียน Bash 101 Hacks eBook ที่มี 101 ตัวอย่างปฏิบัติทั้ง Command Line และ Shell Scripting หากคิดจะ Master Bash ให้ทำความดีใจตัวเองและอ่านหนังสือเล่มนี้ จะช่วยให้ควบคุม Bash Command Line และ Shell Scripting ได้อย่างเต็มที่