shell脚本(13)-shell函数

一、函数介绍

将相同功能的代码模块化,使得代码逻辑上比较简单,代码量少,排错容易

函数的优点:

1、代码模块化,调用方便,节省内存

2、代码模块化,代码量少,排错简单

3、代码模块化,可以改变代码的执行顺序

 

二、函数语法

1、语法一

函数名 () {
    代码块
    return N
}

2、语法二

function 函数名 {
    代码块
    return N
}

 

三、函数应用

复制代码
#!/usr/bin/bash
#################################
# Author: Mr.white #
# Create_Date: 2021-07-30 22:55:15 #
# Version: 1.0 #
#################################

#定义函数
start () {
    echo "Apache start......          [OK]"
    #return 0
}

function stop {
    echo "Apache stop ......           [FAIL]"
}

#调用函数
start
stop
复制代码

查询运行结果:

[root@localhost test20210730]# sh fun1.sh
Apache start...... [OK]
Apache stop ...... [FAIL]

 

四、实战:编写nginx启动管理脚本

复制代码
#!/usr/bin/bash
#################################
# Author: Mr.white #
# Create_Date: 2021-07-31 01:16:59 #
# Version: 1.0 #
#################################
#nginx seivice manage script

#varibles
nginx_install_doc=/usr/local/nginx
nginxd=$nginx_install_doc/sbin/nginx
pid_file=$nginx_install_doc/logs/nginx.pid

#参考/etc/init.d/network中以下函数库
# Source function library.
if [ -f /etc/init.d/functions ];then
  . /etc/init.d/functions
else
  echo "not found file /etc/init.d/functions"
  exit
fi

if [ -f $pid_file ];then
  nginx_process_id=`cat $pid_file`
  nginx_process_num=`ps aux |grep $nginx_process_id | grep -v grep|wc -l`
fi
#function

start () {
#判断nginx没有启动直接启动,否则报错已经启动
  if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
    echo "nginx running..."
  else
    if [ -f $pid_file ]&&[ $nginx_process_num -lt 1 ];then
      rm -f $pid_file
      echo " nginx start `daemon $nginxd` " 
    fi
    echo " nginx start `daemon $nginxd` " 
  fi
}

stop () {
  if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
    action "nginx stop" killall -s QUIT $nginxd
      #rm -f $pid_file
  else
    action "nginx stop" killall -s QUIT $nginxd 2>/dev/null
  fi
}

restart () {
  stop
  sleep 1
  start
}

reload () {
  if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
    action "nginx reload" killall -s HUP nginx
  else
    action "nginx reload" killall -s HUP nginx 2>/dev/null
  fi
}

status () {
  if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];then
    echo "nginx running..."
  else
    echo "nginx stop"
  fi
}


#callable
case $1 in
start) start;;
stop) stop;;
restart) restart;;
reload) reload;;
status) status;;
*) echo "USAGE: $0 start|stop|restart|reload|status";;
esac
复制代码

查看运行结果:

复制代码
[root@localhost test20210731]# ps aux | grep nginx | grep -v grep
[root@localhost test20210731]# sh nginxd.sh #使用帮忙
USAGE: nginxd.sh start|stop|restart|reload|status
[root@localhost test20210731]# sh nginxd.sh start #开启进程
 nginx start                                               [  OK  ]
[root@localhost test20210731]# sh nginxd.sh status #查看状态为开启状态
nginx running...
[root@localhost test20210731]# sh nginxd.sh start #提示已开启
nginx running...
[root@localhost test20210731]# sh nginxd.sh reload #重载进程
nginx reload                                               [  OK  ]
[root@localhost test20210731]# sh nginxd.sh restart #重启进程
nginx stop                                                 [  OK  ]
 nginx start                                               [  OK  ]
[root@localhost test20210731]# sh nginxd.sh stop #关闭进程
nginx stop                                                 [  OK  ]
[root@localhost test20210731]# sh nginxd.sh status #查看状态为关闭状态
nginx stop
[root@localhost test20210731]# sh nginxd.sh stop #提示已经关闭无法再关闭
nginx stop                                                 [FAILED]
[root@localhost test20210731]# sh nginxd.sh reload #提示已经关闭无法重载
nginx reload                                               [FAILED]
[root@localhost test20210731]# sh nginxd.sh restart  #提示已经关闭无法重启
nginx stop                                                 [FAILED]
 nginx start                                               [  OK  ]
复制代码

 添加到系统服务启动管理

复制代码
[root@localhost test20210731]# cp -r nginxd.sh /etc/init.d/nginxd
[root@localhost test20210731]# chmod 755 /etc/init.d/nginxd
[root@localhost test20210731]# service nginxd status
nginx running...
[root@localhost test20210731]# service nginxd stop
Stopping nginxd (via systemctl):  Warning: nginxd.service changed on disk. Run 'systemctl daemon-reload' to reload units.
                                                           [  确定  ]
[root@localhost test20210731]# service nginxd status
nginx stop
[root@localhost test20210731]# service nginxd start
Starting nginxd (via systemctl):                           [  确定  ]
[root@localhost test20210731]# service nginxd status
nginx running...
复制代码

 

posted @   Mrwhite86  阅读(74)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示