1. 問題背景

之前寫的 script 都是「一行行執行到底」,沒有任何判斷。

但有時候我們希望:

這時就要用到 if statement


2. 基本語法

if [ condition ]
then
    commands
fi


3. 範例:判斷輸入的數字是否大於 100

#!/bin/bash

read -p "Enter a number: " NUM

if [ $NUM -gt 100 ]
then
    echo "You have entered the IF block."
    sleep 3
    echo "Your number is greater than 100."
    echo
    date
fi

echo "Execution completed."

說明: