通过代码来给大家做个示例。
- #!/bin/bash
- function install_mysql(){
- echo mysql ready.
- }
- function install_php(){
- echo php ready.
- }
- function install_nginx(){
- echo nginx ready.
- }
- case $1 in
- install_mysql)
- install_mysql;;
- install_php)
- install_php;;
- install_nginx)
- install_nginx;;
- *)
- echo "Usage:`basename $0` {install_mysql|install_php|install_nginx}";;
- esac
下面给大家贴一个shell中case的一个语法结构
- case $arg in
- pattern | sample) # arg in pattern or sample
- ;;
- pattern1) # arg in pattern1
- ;;
- *) #default
- ;;
- esac
大家可以看到:case中是可以使用正则表达式的,这是很多其他语言所不具备的。
执行结果给大家贴一下:
- [root@localhost sh]# sh function.sh
- Usage:function.sh {install_mysql|install_php|install_nginx}
- [root@localhost sh]# sh function.sh install_mysql
- mysql ready.
转载于:https://blog.51cto.com/divinesoft/1068402