-eq
(==)-ne
(!=)-gt
(>)-ge
(>=)-lt
(<)-le
(<=)
常用指令
- 顯示文字
echo "要顯示的字串"
- 讀取使用者輸入的內容
read [-p <提示符號>] [-t <等待秒數>] [變數名稱]
=> read -p "請輸入姓名:" answer // 後面的程式可利用$answer來取用使用者所輸入的資料
條件判斷式 (if ... then ... fi)
- 範例1
if [ "$a" -eq "$b" || "$b" -eq "123" ]; then
echo "$b"
exit 0
fi - 範例2
if [ "$a" -eq "$b" && "$b" -eq "123" ]; then
echo "$b"
exit 0
elif [ "$a" -eq "$c" ]; then
echo "$a"
else
echo "not match"
fi
補充說明:
條件判斷式 (case ... esac)
- 範例1
case $a in
"1")
echo "situation $a"
;;
"2")
echo "case 2"
;;
*)
echo "not in any case above"
;;
esac
迴圈 (while do done / until do done)
- 範例
until [ "$a" = "y" ]
do
read -p "you have to input y to stop this!!" $a
done - i=1
s=0
while [ "$i" != "10" ]
do
i=$(($i+1))
s=$(($s+$i))
done
echo $s