码迷,mamicode.com
首页 > 其他好文 > 详细

05 函数

时间:2021-06-17 16:37:05      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:function   bin   定义   pre   env   logs   not found   sbin   top   

函数的定义

# 方式一
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 函数

标签:function   bin   定义   pre   env   logs   not found   sbin   top   

原文地址:https://www.cnblogs.com/zhaokunhao/p/14890998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!