05 函数
Posted baim0的blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了05 函数相关的知识,希望对你有一定的参考价值。
函数的定义
# 方式一
start () {
echo "nginx start .... [OK]"
#return 0
}
stop () {
echo "nginx stop .... [FAIL]"
}
# 方式二
function start {
echo "nginx start .... [OK]"
}
# 函数的调用
start
stop
案例-编译安装nginx启动jio本
#!/usr/bin/env bash
nginx_install_doc=/usr/local/nginx
nginxd=${nginx_install_doc}/sbin/nginx
pid_file=${nginx_install_doc}/logs/nginx.pid
if [ -f $pid_file ]; then
nginx_pid=`cat ${pid_file}`
nginx_num=`ps -ef | grep ${nginx_pid} | grep -v "grep" | wc -l`
fi
if [ -f /etc/init.d/functions ]; then
. /etc/init.d/functions
else
echo "not found file /etc/init.d/functions"
exit
fi
start() {
if [ -f ${pid_file} ] && [ ${nginx_num} -ge 1 ]; then
echo "nginx running..."
else
if [ -f $pid_file ] && [ $nginx_num -lt 1 ]; then
rm -rf $pid_file
echo " nginx start `daemon $nginxd` "
fi
action "nginx start" $nginxd
fi
}
stop() {
if [ -f $pid_file ] && [ $nginx_num -ge 1 ]; then
action "nginx stop" pkill nginx
else
action "nginx stop" pkill nginx &> /dev/null
fi
}
restart() {
stop
sleep 1
start
}
status() {
if [ -f $pid_file ] && [ $nginx_num -ge 1 ]; then
echo "nginx running"
else
echo "nginx stop"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "错误输入,请重新执行"
;;
esac
以上是关于05 函数的主要内容,如果未能解决你的问题,请参考以下文章