if 語句在 shell 腳本中的基本語法為:
if [ condition ]; then
# code to be executed if condition is true
fi
其中 condition 是要判斷的條件,可以是比較兩個值、檢查變量是否為空等等。如果 condition 的結果為 true,則執行 then 和 fi 之間的代碼塊。
例如,判斷一個變量是否等于某個值:
fruit="apple"
if [ $fruit == "apple" ]; then
echo "This is an apple"
fi
注意,if 語句中的條件表達式需要用方括號 [] 括起來,并且變量的值前后都需要空格。