AI智能
改变未来

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#variblesnginx_install_doc=/usr/local/nginxnginxd=$nginx_install_doc/sbin/nginxpid_file=$nginx_install_doc/logs/nginx.pid#参考/etc/init.d/network中以下函数库# Source function library.if [ -f /etc/init.d/functions ];then. /etc/init.d/functionselseecho \"not found file /etc/init.d/functions\"exitfiif [ -f $pid_file ];thennginx_process_id=`cat $pid_file`nginx_process_num=`ps aux |grep $nginx_process_id | grep -v grep|wc -l`fi#functionstart () {#判断nginx没有启动直接启动,否则报错已经启动if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];thenecho \"nginx running...\"elseif [ -f $pid_file ]&&[ $nginx_process_num -lt 1 ];thenrm -f $pid_fileecho \" nginx start `daemon $nginxd` \"fiecho \" nginx start `daemon $nginxd` \"fi}stop () {if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];thenaction \"nginx stop\" killall -s QUIT $nginxd#rm -f $pid_fileelseaction \"nginx stop\" killall -s QUIT $nginxd 2>/dev/nullfi}restart () {stopsleep 1start}reload () {if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];thenaction \"nginx reload\" killall -s HUP nginxelseaction \"nginx reload\" killall -s HUP nginx 2>/dev/nullfi}status () {if [ -f $pid_file ]&&[ $nginx_process_num -ge 1 ];thenecho \"nginx running...\"elseecho \"nginx stop\"fi}#callablecase $1 instart) 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 statusnginx running...[root@localhost test20210731]# service nginxd stopStopping nginxd (via systemctl):  Warning: nginxd.service changed on disk. Run \'systemctl daemon-reload\' to reload units.[  确定  ][root@localhost test20210731]# service nginxd statusnginx stop[root@localhost test20210731]# service nginxd startStarting nginxd (via systemctl):                           [  确定  ][root@localhost test20210731]# service nginxd statusnginx running...

赞(0) 打赏
未经允许不得转载:爱站程序员基地 » shell脚本(13)-shell函数