17.select 選單語法

select 可方便製作一個簡易的選單。

select 的寫法

#! /bin/sh

select fn in *; do
    # 若不在選項中, 則離去
    if [ -z $fn ]; then
	break
    fi
    echo "$fn (you choose $REPLY)"
done

# 若 select 及 done 中間沒有 break, 則會不斷地 loop
# 選項數字被存入 $REPLY 中

=======================

結果如下:

1) dataf          5) Factor2.sh    9) function.sh  13) sysinfo.sh
2) exp.sh         6) Factor3.sh   10) picts        14) test2.sh
3) Factor0.sh     7) Factor.sh    11) select.sh
4) factor2.sh     8) f.sh         12) sel.sh
#? 2
exp.sh (you choose 2)
1) dataf          5) Factor2.sh    9) function.sh  13) sysinfo.sh
2) exp.sh         6) Factor3.sh   10) picts        14) test2.sh
3) Factor0.sh     7) Factor.sh    11) select.sh
4) factor2.sh     8) f.sh         12) sel.sh
#?