歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> SHELL編程 >> Shell case語句用法小結

Shell case語句用法小結

日期:2017/2/28 15:56:38   编辑:SHELL編程

在shell編程中,對於多分支判斷,用if 雖然也可以實現,但有些時候,寫起來很麻煩,也不容易代碼理解。這個時候,可以考慮case。大道理不講了,先給出個簡單的demo吧

[plain]
  1. #! /bin/sh -
  2. name=`basename $0 .sh`
  3. case $1 in
  4. s|start)
  5. echo "start..."
  6. ;;
  7. stop)
  8. echo "stop ..."
  9. ;;
  10. reload)
  11. echo "reload..."
  12. ;;
  13. *)
  14. echo "Usage: $name [start|stop|reload]"
  15. exit 1
  16. ;;
  17. esac
  18. exit 0

注意:1、*) 相當於其他語言中的default。

2、除了*)模式,各個分支中;;是必須的,;;相當於其他語言中的break

3、 | 分割多個模式,相當於or

Copyright © Linux教程網 All Rights Reserved