53 lines
1.1 KiB
Markdown
53 lines
1.1 KiB
Markdown
### 语法结构
|
|
- 多重匹配语句
|
|
- 如果匹配成功,执行相匹配的命令
|
|
```
|
|
pattern 表示需要匹配的模式
|
|
|
|
case var in
|
|
pattern 1) # 可以用 | 分割多个模式,相当于 or
|
|
command 1 # 需要执行的语句
|
|
;; # 两个分号代表命令结束
|
|
pattern 2)
|
|
command 2
|
|
;;
|
|
*) # default 不满足以上模式,默认执行下边的语句
|
|
command 3
|
|
;;
|
|
esac
|
|
```
|
|
|
|
```
|
|
#! /bin/env bash
|
|
|
|
case $1 in
|
|
start|S)
|
|
echo "service starting"
|
|
;;
|
|
stop|T)
|
|
echo "service stopping"
|
|
;;
|
|
reload|R)
|
|
echo "service restarting"
|
|
;;
|
|
*)
|
|
echo "invalid params: $1"
|
|
;;
|
|
esac
|
|
```
|
|
|
|
```
|
|
me@me-EQ59:~/shell_demo/scripts/shell03$ ./case_1.sh
|
|
invalid params:
|
|
me@me-EQ59:~/shell_demo/scripts/shell03$ ./case_1.sh 2
|
|
invalid params: 2
|
|
me@me-EQ59:~/shell_demo/scripts/shell03$ ./case_1.sh start
|
|
service starting
|
|
me@me-EQ59:~/shell_demo/scripts/shell03$ ./case_1.sh S
|
|
service starting
|
|
me@me-EQ59:~/shell_demo/scripts/shell03$ ./case_1.sh reload
|
|
service restarting
|
|
me@me-EQ59:~/shell_demo/scripts/shell03$ ./case_1.sh T
|
|
service stopping
|
|
me@me-EQ59:~/shell_demo/scripts/shell03$
|
|
``` |