之前寫的 script 都是「一行行執行到底」,沒有任何判斷。
但有時候我們希望:
這時就要用到 if statement。
if [ condition ]
then
commands
fi
if 開始條件[ condition ] 是判斷(注意:中括號兩邊要有空格)then 是條件成立要執行的區塊fi 表示 if 的結束(fi = if 反過來)#!/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."
說明:
gt 是「greater than」(>)lt :less than (<)eq :equal (=)ne :not equal (!=)