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...
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了