一、函数介绍

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

函数的优点:

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

shell脚本(13)-shell函数的更多相关文章

  1. shell编程基础(四): shell脚本语法之函数及调试

    一.Shell脚本中的函数 和C语言类似,Shell中也有函数的概念,但是函数定义中没有返回值也没有参数列表.例如: #! /bin/sh fun(){ echo "Function fun ...

  2. shell脚本学习—Shell执行脚本

    Shell作用是解释执行用户的命令,用户输入一条命令,Shell就解释执行这一条,这种方式称为交互式,但还有另一种执行命令的方式称为批处理方式,用户事先写一个Shell脚本,Shell可以一次把这些命 ...

  3. Linux python3安装/shell脚本/if/循环/函数

    python3安装 安装过程 安装包: wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgztar -xvf Python-3.7 ...

  4. Linux&shell之高级Shell脚本编程-创建函数

    写在前面:案例.常用.归类.解释说明.(By Jim) 使用函数 #!/bin/bash # testing the script function myfun { echo "This i ...

  5. 在shell脚本中使用函数

    转载请标明:http://www.cnblogs.com/winifred-tang94/ 对于在脚本中重复使用的功能模块,可以封装成为函数. shell脚本中函数的定义可以使用如下两种方式: a. ...

  6. 【shell脚本】shell脚本实现的 函数差集查找

    文本地址 点击关注微信公众号 wenyuqinghuai 分享提纲: 1. 问题背景 2. 代码实现 1.问题背景 在做公司的测试的自动化测试时,覆盖了一些开发代码的函数,但是那些还没有做,使用一个函 ...

  7. linux shell 进阶篇、shell脚本编程-创建函数

    使用函数 #!/bin/bash # testing the script function myfun { echo "This is an example of a function&q ...

  8. 8、在Shell脚本中使用函数

    学习目标Shell的函数 Shell程序也支持函数.函数能完成一特定的功能,可以重复调用这个函数.函数格式如下: 函数名() {     函数体 }   函数调用方式: 函数名 参数列表   实例:编 ...

  9. Shell脚本之:函数

    Shell 也支持函数.Shell函数必须先定义后使用. 函数的定义与调用 Shell 函数的定义格式如下: function_name () { list of commands [ return ...

  10. 分享7个shell脚本实例--shell脚本练习必备

    概述 看多shell脚本实例自然就会有shell脚本的编写思路了,所以我一般比较推荐看脚本实例来练习shell脚本.下面分享几个shell脚本实例. 1.监测Nginx访问日志502情况,并做相应动作 ...

随机推荐

  1. [USACO2007NOVG] Telephone Wire G

    题目描述 Farmer John's cows are getting restless about their poor telephone service; they want FJ to rep ...

  2. 本地部署modelscope-agent

    本地部署modelscope-agent 部署流程 在modelscope社区创建一个自己的空间(假设name是LocalAgent),clone空间到本地(或云服务器如魔搭Notebook) git ...

  3. python操作redis集群、redis主从+哨兵

    主从+哨兵 from redis.sentinel import Sentinel if __name__ == '__main__': # 哨兵监听的别名,这个就是你redis配置中的名字 serv ...

  4. bash shell笔记整理——which和whereis命令

    which和whereis命令作用 which:显示给定命令所在路径 whereis:相比which更完善,显示命令路径.man文件路径(如果有).源代码路径 which语法 which [optio ...

  5. React 类组件转换为函数式

    函数式的 React 组件更加现代,并支持有用的 hooks,现在流行把旧式的类组件转换为函数式组件.这篇文章总结了转换的一些通用的步骤和陷阱. 通用替换 定义 从 class (\w+) exten ...

  6. JavaFx之播放MP4(二十七)

    JavaFx之播放MP4(二十七) JavaFX 视频和音频支持,由 JavaFX 媒体类 Media.MediaPlayer.MediaView 和 AudioClip 提供. import jav ...

  7. 根据图片URL地址下载图片

    /// <summary> /// 下载图片 /// </summary> /// <param name="picUrl">图片Http地址& ...

  8. 一些Mybatis的知识点&易错点总结

    1.映射文件配置容易出错 在映射文件中,我们习惯性在sql语句后面添加';'. 结果是报了一堆错误: org.apache.ibatis.exceptions.PersistenceException ...

  9. 一些JavaSE学习过程中的思路整理(三)(主观性强,持续更新中...)

    目录 一些JavaSE学习过程中的思路整理(三)(主观性强,持续更新中...) Java线程同步的几种常见情况分析 由简单到复杂的几种单例模式写法 死锁的实现与破解 使用lambda表达式化简代码 J ...

  10. 【scikit-learn基础】--『监督学习』之 均值聚类

    聚类算法属于无监督学习,其中最常见的是均值聚类,scikit-learn中,有两种常用的均值聚类算法:一种是有名的K-means(也就是K-均值)聚类算法,这个算法几乎是学习聚类必会提到的算法:另一个 ...