一、case语句格式
case 变量 in 模式)
命令序列1
;;模式2)
命令序列2
;;模式3)
命令序列3
esac
二、函数
- 函数格式
[ function ] 函数名 (){
函数体
[return x] #返回值
} - 函数调用方法
函数名 [ 参数 ]
三、服务启动实例nginx
例:
#!/bin/bash
#nginx服务启动程序
#DATA:2019-9-9
#变量
nginxurl="/usr/local/nginx/sbin/nginx"
#启动服务函数
startNginx(){
netstat -lunpt | grep -q nginx
if [ $? -ne 0 ];then #nginx服务未开启,开启
/usr/local/nginx/sbin/nginx &> /dev/null
else
return 0
fi
}
#关闭服务
stopNginx(){
netstat -lunpt | grep -q nginx
if [ $? -eq 0 ];then #nginx开启关闭
/usr/local/nginx/sbin/nginx -s stop &> /dev/null
else
return 0
fi
}
#重启服务
restartNginx(){
stopNginx
startNginx
}
#刷新配置
reloadNginx(){
netstat -lunpt | grep -q nginx
if [ $? -eq 0 ];then #nginx开启,刷新配置
/usr/local/nginx/sbin/nginx -s reload
else #不然先启动,后刷新
startNginx
reloadNginx
fi
}
#其它
qita(){
echo "Usage: /etc/init.d/nginx {start|stop|restart|reload}"
}
case $1 in start)
startNginx #调用启动函数
;;stop)
stopNginx #调用关闭函数
;;reload)
reloadNginx #调用刷新配置函数
;;restart)
restartNginx #调用重启函数
;;*)
qita
esac