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

Shell if...elif...fi 語句

if...elif...fi 語句是提前一個(gè)級(jí)別的控制語句,形式出幾個(gè)條件,允許 Shell 作出正確的決定。

語法

if [ expression 1 ]
then
   Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
   Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then
   Statement(s) to be executed if expression 3 is true
else
   Statement(s) to be executed if no expression is true
fi

這段代碼沒有什么特別的。這僅僅是一個(gè)系列,if 語句每一個(gè)語句else子句的一部分。下面語句是執(zhí)行的基礎(chǔ)上的真實(shí)情況,如果條件不為 ture,則執(zhí)行else塊。

例子:

#!/bin/sh

a=10
b=20

if [ $a == $b ]
then
   echo "a is equal to b"
elif [ $a -gt $b ]
then
   echo "a is greater than b"
elif [ $a -lt $b ]
then
   echo "a is less than b"
else
   echo "None of the condition met"
fi

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

a is less than b