Shell select语句

select语句

select是循环选择,一种bash的扩展应用,一般与case语句使用。
语句格式如下:

select 变量name  in seq变量
do
    action
done

脚本示例如下:

#!/bin/bash

select ch in "begin" "end" "exit"
do
  case $ch in
  "begin")
      echo "start something"
      ;;
   "end")
      echo "stop something"
      ;;
    "exit")
      echo "exit"
      break
      ;;
    *)
      echo "Ignorant"
      ;;
  esac
done

脚本运行结果如下:

1) begin
2) end
3) exit
#? begin
Ignorant
#?
1) begin
2) end
3) exit
#? 1
start something
#? 2
stop something
#? 3
exit