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...
shell脚本(13)-shell函数的更多相关文章
- shell编程基础(四): shell脚本语法之函数及调试
一.Shell脚本中的函数 和C语言类似,Shell中也有函数的概念,但是函数定义中没有返回值也没有参数列表.例如: #! /bin/sh fun(){ echo "Function fun ...
- shell脚本学习—Shell执行脚本
Shell作用是解释执行用户的命令,用户输入一条命令,Shell就解释执行这一条,这种方式称为交互式,但还有另一种执行命令的方式称为批处理方式,用户事先写一个Shell脚本,Shell可以一次把这些命 ...
- Linux python3安装/shell脚本/if/循环/函数
python3安装 安装过程 安装包: wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgztar -xvf Python-3.7 ...
- Linux&shell之高级Shell脚本编程-创建函数
写在前面:案例.常用.归类.解释说明.(By Jim) 使用函数 #!/bin/bash # testing the script function myfun { echo "This i ...
- 在shell脚本中使用函数
转载请标明:http://www.cnblogs.com/winifred-tang94/ 对于在脚本中重复使用的功能模块,可以封装成为函数. shell脚本中函数的定义可以使用如下两种方式: a. ...
- 【shell脚本】shell脚本实现的 函数差集查找
文本地址 点击关注微信公众号 wenyuqinghuai 分享提纲: 1. 问题背景 2. 代码实现 1.问题背景 在做公司的测试的自动化测试时,覆盖了一些开发代码的函数,但是那些还没有做,使用一个函 ...
- linux shell 进阶篇、shell脚本编程-创建函数
使用函数 #!/bin/bash # testing the script function myfun { echo "This is an example of a function&q ...
- 8、在Shell脚本中使用函数
学习目标Shell的函数 Shell程序也支持函数.函数能完成一特定的功能,可以重复调用这个函数.函数格式如下: 函数名() { 函数体 } 函数调用方式: 函数名 参数列表 实例:编 ...
- Shell脚本之:函数
Shell 也支持函数.Shell函数必须先定义后使用. 函数的定义与调用 Shell 函数的定义格式如下: function_name () { list of commands [ return ...
- 分享7个shell脚本实例--shell脚本练习必备
概述 看多shell脚本实例自然就会有shell脚本的编写思路了,所以我一般比较推荐看脚本实例来练习shell脚本.下面分享几个shell脚本实例. 1.监测Nginx访问日志502情况,并做相应动作 ...
随机推荐
- MySQL运维5-Mycat配置
一.schema.xml 1.1 简介 schema.xml作为Mycat中最重要的配置文件之一,涵盖了Mycat的逻辑库.逻辑表.分片规则.分片节点即数据源的配置.主要包括一下三组标签 schema ...
- PyTorch 中自定义数据集的读取方法
显然我们在学习深度学习时,不能只局限于通过使用官方提供的MNSIT.CIFAR-10.CIFAR-100这样的数据集,很多时候我们还是需要根据自己遇到的实际问题自己去搜集数据,然后制作数据集(收集数据 ...
- Linux查看内存使用情况以及释放内存
查看内存使用情况 一般使用 top 命令即可,命令行输入 top会实时显示内存和cpu等硬件的使用情况,然后输入M即显示内存的使用情况. 但是有些情况下,内存明明被占用了,但是使用top命令依然无法找 ...
- 开心自走棋:使用 Laf 云开发支撑数百万玩家
先介绍一下开心自走棋 开心自走棋是一款剑与魔法的烧脑自走棋游戏.以著名的魔幻世界观为蓝本,采用了轻松可爱的画面风格,精致细腻的动画和特效来还原魔兽之战. 现在市面上自走棋游戏多是 PvP 玩法为主,而 ...
- javaGUI入门之swing(一)
javaGUI入门之swing(一) 前面学过javafx,发现他们有"异曲同工"之处,demo写起来也类似,无非是类名不一样.个人觉得实现一个桌面应用不应该只看用一种语言一种框架 ...
- VSCode C++开发环境配置:CMake 调试配置 launch.json
相关内容 VSCode C++开发环境配置: LLVM clang clangd 安装 cmake sudo apt install cmake 安装 VSCode 插件 CMake CMakeToo ...
- kylin&CDH理论基础
Kylin&CDH理论基础 一.维度与度量 维度是观察数据的角度.比如电商的销售数据,可以从时间维度来观察,进一步细化时间和地区维度来观察. 度量是被聚合的统计值,也是聚合运算的结果.知道维度 ...
- Spring Boot 应用部署流程
在本机操作,执行以下步骤启动项目 1.从Github上下载源码 git clone https://github.com/ChinaSilence/any-video.git 2.在数据库中建库,建表 ...
- Java 查找并高亮PDF中的跨行文本
以下内容介绍如何在Java后端程序中查找并高亮PDF文档中的跨行文本.本次测试环境如下: 源文档:PDF 编译工具:IntelliJ IDEA2018 JDK:1.8.0 PDF类库:free spi ...
- 最后一天,CDMP数据治理认证高分训练营(9-10月)开放报名!
大家好,我是独孤风,一位曾经的港口煤炭工人,目前在某国企任大数据负责人,公众号大数据流动主理人. 首先告诉大家一个好消息,在大家共同的努力下,我们的CDMP国际数据治理认证训练营(7-8月)基本收官, ...