一、函数介绍

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

函数的优点:

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. C# 从代码入门 Mysql 数据库事务

    目录 生成数据库数据 Mysql 数据库事务基础 数据库的并发一致性问题 数据库事务的隔离级别 BeginTransaction() 和 TransactionScope 的区别 BeginTrans ...

  2. 聊一聊 .NET高级调试 内核模式堆泄露

    一:背景 1. 讲故事 前几天有位朋友找到我,说他的机器内存在不断的上涨,但在任务管理器中查不出是哪个进程吃的内存,特别奇怪,截图如下: 在我的分析旅程中都是用户态模式的内存泄漏,像上图中的异常征兆已 ...

  3. MongoDB中的分布式集群架构

    MongoDB 中的分布式集群架构 前言 Replica Set 副本集模式 副本集写和读的特性 Sharding 分片模式 分片的优势 MongoDB 分片的组件 分片键 chunk 是什么 分片的 ...

  4. 一个Servlet如何实现增-删-改-查的业务逻辑

    一.业务场景 最近在教学生学习JavaWeb中的Servlet,它就是一个Java服务端的小程序,用来提供各种服务. 在讲解得时候,自己突然遇到一个问题,那就是现在没有使用什么SpringMvc框架, ...

  5. 【笔记】负载均衡Robbin之不同服务使用不同的策略

    裂开裂开,搞这么久忘记导入依赖 妈卖批 又不报错 还能让我玩 我以为全部导入了. 话不多说,开始演示. 介绍 给不同的服务 配置 不同的 负载均衡策略 这里使用 用户模块 进行访问其它两个模块的con ...

  6. 升级高版本springboot2.6.x:org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata

    升级springboot高版本2.6.x 项目使用到了springcloud的oauth2依赖,直接升级springboot项目版本为最新 2.6.8(2022年6月16日)将会报以下错误: org/ ...

  7. 12、FlutterMediaQuery获取屏幕宽度和高度

    final size =MediaQuery.of(context).size; class HomePage3 extends StatelessWidget { const HomePage3({ ...

  8. Llama2-Chinese项目:6-模型评测

      测试问题筛选自AtomBulb[1],共95个测试问题,包含:通用知识.语言理解.创作能力.逻辑推理.代码编程.工作技能.使用工具.人格特征八个大的类别. 1.测试中的Prompt   例如对于问 ...

  9. Navicat 携手华为云GaussDB,联合打造便捷高效的数据库开发和建模工具方案

    本文分享自华为云社区<Navicat 携手华为云GaussDB,联合打造便捷高效的数据库开发和建模工具方案>,作者: GaussDB 数据库 . 近日, Navicat Premium顺利 ...

  10. 开抢| 华为开发者大会2021(Cloud)早鸟票来了!

    他们,是技术的引领者 他们,是科学的追随者 他们用代码守护蓝色星球的生态 破解宇宙深处的密码 化解城市生活的不畅 捉摸自然气候的脾性 ...... 这些改变世界的成果背后 每一个开发者都了不起 --那 ...