這里使用shell中的case語法:
case分支語句格式如下:
case $變量名 in
模式1)
命令列表
;;
模式2)
命令列表
;;
*)
;;
esac
case行尾必須為單詞“in”,每一個模式必須以右括號“)”結束。
雙分號“;;”表示命令序列結束。這里給一個編寫應用程序的start、stop、restart等操作的模板
#!/bin/sh BASE_HOME=/home/apple/test PID=${BASE_HOME}/.pid status(){ echo "==========status=======" } start() { echo "==========start==========="; } stop() { echo "===========stop============"; } restart() { stop; echo "sleeping........."; sleep 3; start; } case "$1" in 'start') start ;; 'stop') stop ;; 'status') status ;; 'restart') restart ;; *) echo "usage: $0 {start|stop|restart|status}" exit 1 ;; esac
