一、语法

select 变量 in con1 con2 con3    # 自动列出 con1,con2,con3 的选择菜单
do
#执行内容
break # select本身就是一个循环,break是当选择后,就跳出循环
done

二、练习

  1. select + case 模拟 Linux 启动脚本

    function programstatus(){
    if [ "$1" -eq ]; then
    echo "* program is running"
    else
    echo "* program has stopped"
    fi
    } status= #: start; :stop
    select p in "start" "stop" "status" "restart" "*" ""
    do
    case "$p" in
    "start")
    status=
    programstatus $status
    ;;
    "stop")
    status=
    programstatus $status
    ;;
    "status")
    programstatus $status
    ;;
    "restart")
    if [ "$status" -eq ]; then
    status=
    echo "* program has stopped"
    fi
    status=
    echo "* program is running"
    ;;
    "")
    break
    ;;
    *)
    echo "Plz input [start|stop|status|restart]"
    ;;
    esac
    done
    user@ae01:~$ ./test.sh
    ) start
    ) stop
    ) status
    ) restart
    )
    #?
    * program is running
    #?
    * program has stopped
    #?
    * program has stopped
    #?
    * program is running
    #?
    user@ae01:~$