select也是循环的一种,它比较适合用在用户选择的情况下。
比如,我们有一个这样的需求,运行脚本后,让用户去选择数字,选择1,会运行w命令,选择2运行top命令,选择3运行free命令,选择4退出。脚本这样实现:
1. #!/bin/bash
2. echo "Please chose a number, 1: run w, 2: run top, 3: run free, 4: quit"
3. echo
4. select command in w top free quit
5. do
6. case $command in
7. w)
8. w
9. ;;
10. top)
11. top
12. ;;
13. free)
14. free
15. ;;
16. quit)
17. exit
18. ;;
19. *)
20. echo "Please input a number:(1-4)."
21. ;;
22. esac
23. done
转载于:https://blog.51cto.com/amazing61/2063860