一、函数介绍

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

函数的优点:

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. [ABC262E] Red and Blue Graph

    Problem Statement You are given a simple undirected graph with $N$ vertices and $M$ edges. The verti ...

  2. 【scikit-learn基础】--『预处理』之 分类编码

    数据的预处理是数据分析,或者机器学习训练前的重要步骤.通过数据预处理,可以 提高数据质量,处理数据的缺失值.异常值和重复值等问题,增加数据的准确性和可靠性 整合不同数据,数据的来源和结构可能多种多样, ...

  3. 36. 干货系列从零用Rust编写负载均衡及代理,内网穿透中内网代理的实现

    wmproxy wmproxy已用Rust实现http/https代理, socks5代理, 反向代理, 静态文件服务器,四层TCP/UDP转发,七层负载均衡,内网穿透,后续将实现websocket代 ...

  4. 2023-12-23:用go语言,一支n个士兵的军队正在趁夜色逃亡,途中遇到一条湍急的大河 敌军在T的时长后到达河面,没到过对岸的士兵都会被消灭 现在军队只找到了1只小船,这船最多能同时坐上2个士兵。

    2023-12-23:用go语言,一支n个士兵的军队正在趁夜色逃亡,途中遇到一条湍急的大河 敌军在T的时长后到达河面,没到过对岸的士兵都会被消灭 现在军队只找到了1只小船,这船最多能同时坐上2个士兵. ...

  5. 2023计算机保研经验贴 直博向(南大cs,计算所,科大高研院,浙大cs,交大cs,国科cs,北大cs,清华cs)

    写在前面 本人作为普通选手,只能将个人经验分享一二,不能代表其他人的想法和意见,望路过的大佬们高抬贵手-,如果有相关老师或者同学认为我违反了保密条例请与我私信联系,我会第一时间删除相关内容. 个人情况 ...

  6. Wifi BSSID获取

    代码很简单,通过wifiManager 获取wifiinfo,从而获取bssid, public static String getWifiSSID(Context context) { String ...

  7. influxdb 进行数据删除和修改

    本文为博主原创,转载请注明出处: 1.条件删除数据 InfluxDB 只支持基于时间的删除操作. 可以使用 DELETE 语句来删除指定时间范围内的数据.例如,以下的 SQL 语句将删除 measur ...

  8. 如何清理Docker不用的Volume

    有一句老话叫:书到用时方恨少. 其实电脑的内存空间也是非常宝贵,特别是MacOS下的docker可以分配的空间更是寸土寸金. 在21年的时候我也遇到过类似的问题,当时的经历记录在这篇博客:https: ...

  9. 案例分享-Exception.getMessage突然为null

    背景 之前做的小工具一个jsqlparse+git做的小工具帮我节省时间摸鱼昨天突然停止工作,看了下jvm并没有退出,但是看日志确实有不少Error输出,虽说是一个普通的NPE,但是分析了一下却疑点重 ...

  10. JAVA17安装体验JFX17抢先体验

    JAVA17安装体验JFX17抢先体验 java17版本是长期支持版,至少更新5年以上.而且商用免费!这里我就来体验一把. 一.下载配置 java 17 官网下载地址:https://www.orac ...