鍍金池/ 教程/ Linux/ Shell if...else...fi 語句
Shell 輸入/輸出重定向
Shell 循環(huán)類型
Shell是什么?
Shell 特殊變量
Shell 算術(shù)運算符示例
Shell 關(guān)系運算符示例
Shell 替代
Shell 函數(shù)
Shell 條件語句
Shell 聯(lián)機幫助
Shell 數(shù)組/Arrays
Shell 布爾運算符范例
Shell
Shell if...elif...fi 語句
Shell case...esac 語句
Shell 使用Shell變量
Shell 文件測試符例子
Shell 基本運算符
Korn Shell 運算符
Shell 字符串運算范例
Shell while 循環(huán)
Shell 引用機制
Shell if...else...fi 語句
Shell select 循環(huán)
C Shell運算符
Shell 循環(huán)控制break/continue
Shell for循環(huán)
Shell until 循環(huán)
Shell if...fi語句

Shell if...else...fi 語句

 if...else...fi 語句是控制語句,它允許下一個表格執(zhí)行語句 Shell 更可控的方式在兩個選擇之間作出決定。

語法

if [ expression ]
then
   Statement(s) to be executed if expression is true
else
   Statement(s) to be executed if expression is not true
fi

 Shell expression 求值。如果結(jié)果值是真實的,給定 statement(s) 被執(zhí)行。如果表達式為 false,則語句將不會被執(zhí)行。 

例子:

如果我們把上面的例子中,那么它可以用更好的方式使用 if...else 語句如下:

#!/bin/sh

a=10
b=20

if [ $a == $b ]
then
   echo "a is equal to b"
else
   echo "a is not equal to b"
fi

這將產(chǎn)生以下結(jié)果:

a is not equal to b