+-
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...