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情况,并做相应动作 ...
随机推荐
- [ABC313F] Flip Machines
Problem Statement There are $N$ cards numbered $1$ through $N$. Each face of a card has an integer w ...
- hello Flask最简单的Flask项目
# 1.导包 from flask import Flask # 2.实例化Flask对象.一般变量名都叫app,大家都是这样用,很多扩展插件的文档也是叫app,所以统一都叫app. # __name ...
- bash shell笔记整理——ls命令
语法: ls [选项] [文件 或 目录] 选项 使用说明 –a 显示指定目录下的所有文件,包括隐藏文件. -A 显示除了.和..的外的所有文件. -l 显示详细的文件信息. -d 如果是目录,只显示 ...
- grpc是基于http/2协议的高性能的rpc框架
师傅领进门,修行在个人,跟着官方脚手架demo了grpc后,之后就需要扩展前后知识边界,下面总结grpc的前世今生和最佳实践. https://www.cnblogs.com/JulianHuang/ ...
- 内网& 公网
内.外网是相对于防火墙而言的,在防火墙内部叫做内网,反之就是外网.在一定程度上外网等同于公网,内网等同于私网. 内网IP是什么? 内网IP简单理解就是局域网IP地址.内网地址即局域网(LAN),内网的 ...
- 年底了,网站被挂马了,关于IIS被陌生DLL劫持(新人发帖,写的不好的地方,请多多担待)
一上班被分到两个需要杀毒的站点,情况是SEO被劫持 出现一些博彩信息,但是打开确实正常内容,使用站长工具的网站被黑检测功能,发现网站的HEAD前面加载一对加密的东西 一开始我使用D盾扫描网站,删除了一 ...
- Pikachu漏洞靶场 RCE(远程命令执行/代码执行)
RCE 文章目录 RCE 概述 exec "ping" exec "eval" 概述 RCE(remote command/code execute),远程命令 ...
- 对比Spring Boot中的JdbcClient与JdbcTemplate
本文我们一起看看Spring Boot中 JdbcClient 和 JdbcTemplate 之间的差异. 以下内容使用的Java和Spring Boot版本为: Java 21 Spring Boo ...
- LeetCode 图篇
743. 网络延迟时间 有 N 个网络节点,标记为 1 到 N. 给定一个列表 times,表示信号经过有向边的传递时间. times[i] = (u, v, w),其中 u 是源节点,v 是目标节点 ...
- 日常Bug排查-应用Commit报错事务并没有回滚
日常Bug排查-应用Commit报错事务并没有回滚 前言 日常Bug排查系列都是一些简单Bug排查,笔者将在这里介绍一些排查Bug的简单技巧,同时顺便积累素材_. 应用Commit报错并不一定回滚 事 ...