您的位置:首页 > 运维架构 > Shell

使用Shell函数

2019-06-12 09:23 1271 查看
版权声明:苏苏吖 https://blog.csdn.net/weixin_44774638/article/details/91489472

问题
本案例要求编写两个Shell脚本,相关要求如下:
一个funexpr.sh脚本:由用户在执行时提供2个整数值参数,计算这2个整数的加、减、乘、除结果
为源码安装的Web服务编写服务脚本myhttpd:启动、停止操作都使用函数实现;已知httpd安装路径位于/usr/local/httpd
方案
在Shell脚本中,将一些需重复使用的操作,定义为公共的语句块,即可称为函数。通过使用函数,可以使脚本代码更加简洁,增强易读性,提高Shell脚本的执行效率
1)函数的定义方法
格式1:
function 函数名 {
命令序列
… …
}
格式2:
函数名() {
命令序列
… …
}
一般来说,第二种格式比较常见。
比如,以下脚本代码可以定义一个名为helloworld1的函数(格式1),用来输出一段文字“Hello 1 World .”:
function helloworld1 {
echo “Hello 1 World .”
}
以下脚本代码可以定义一个名为helloworld2的函数(格式2),用来显示调用此函数时提供的位置参数($表示所有参数的值):
helloworld2() {
echo "Hello, $"
}
2)函数的调用
直接使用“函数名”的形式调用,如果该函数能够处理位置参数,则可以使用“函数名 参数1 参数2 … …”的形式调用。
注意:函数的定义语句必须出现在调用之前,否则无法执行。
比如,根据前面的函数定义,以下执行语句可以调用helloworld1输出“Hello 1 World .”字符串,根据需要可调用多次:
helloworld1
helloworld1
helloworld1
以下执行语句可以调用helloworld2输出指定的字符串,根据需要每次调用时可以提供不同的输出信息:
helloworld2 “Everybody”
helloworld2 “ladies and gentlemen”
helloworld2 “各位乡亲们 _”
步骤
实现此案例需要按照如下步骤进行。

步骤一:编写funexpr.sh脚本

1)任务需求及思路分析
用户在执行时提供2个整数参数,这个可以通过位置变量$1、$2读入。
针对给定的两个整数,四则运算可以视为一组操作,可以定义为一个函数,依次负责加减乘除运算并输出结果。
调用函数时,将用户提供的两个参数传递给函数处理。
2)根据实现思路编写脚本文件
[root@svr5 ~]# vim funexpr.sh
#!/bin/bash
myexpr() {
echo “$1 + $2 = $[$1+$2]”
echo “$1 - $2 = $[$1-$2]”
echo “$1 * $2 = $[$1*$2]”
echo “$1 / $2 = $[$1/$2]”
}
myexpr $1 $2

[root@svr5 ~]# chmod +x funexpr.sh
3)测试脚本执行效果
[root@svr5 ~]# ./funexpr.sh 43 21
43 + 21 = 64
43 - 21 = 22
43 * 21 = 903
43 / 21 = 2

[root@svr5 ~]# ./funexpr.sh 1234 567
1234 + 567 = 1801
1234 - 567 = 667
1234 * 567 = 699678
1234 / 567 = 2

步骤二:编写myhttpd服务脚本

1)任务需求及思路分析
作为系统服务脚本,应该将脚本复制到/etc/init.d/目录下,还应该在脚本开头添加chkconfig管理参数、description服务描述。服务脚本的控制参数通过位置变量 $1 传入,使用case分支进行识别、执行相应的操作。
简单起见,start、stop操作可直接调用源码安装的httpd的控制程序apachectl,通过if判断执行结果,分别给出“确定”或“失败”的提示。
为了查看及使用方便,将上述start、stop操作分别定义为函数。restart操作则直接先调stop再调start。对于case未识别的其他参数,给出脚本的正确用法。
2)根据实现思路编写脚本文件

[root@svr5 ~]# vim /etc/init.d/myhttpd
#!/bin/bash
# chkconfig: - 85 24
# description: Startup script for httpd server. (for Test only)
PROG="/usr/local/httpd/bin/apachectl"  		//指定web控制程序路径
start() {  									//定义start函数,用来启动服务
echo -n "正在启动httpd服务 ... "
if $PROG start &> /dev/null  			//启动服务并判断是否成功
then
echo -e "\t\t\t\t[确定]"  			//-e启用转义符,\t表示一个制表位
else
echo -e "\t\t\t\t[失败]"
fi
}
stop() {  									//定义stop函数,用来启动服务
echo -n "正在关闭httpd服务 ... "
if $PROG stop &> /dev/null  				//启动服务并判断是否成功
then
echo -e "\t\t\t\t[确定]"
else
echo -e "\t\t\t\t[失败]"
fi
}

case "$1" in
start)
start  									//调用start函数
;;
stop)
stop  									//调用stop函数
;;
restart)
$0 stop
$0 start
;;
*)
echo "用法: $0 {start|stop|restart}"
exit 1
esac
[root@svr5 ~]# chmod +x /etc/init.d/myhttpd

3)验证、测试脚本
确认对start控制参数的响应:
[root@svr5 ~]# netstat -anptl | grep httpd //确认httpd网络服务未监听
[root@svr5 ~]# service myhttpd start
正在启动httpd服务 … [确定]
[root@svr5 ~]# netstat -anptl | grep httpd //再次检查httpd服务已在监听
tcp 0 0 :::80 ::😗 LISTEN 16011/httpd
确认对stop控制参数的响应:
[root@svr5 ~]# service myhttpd stop
正在关闭httpd服务 … [确定]
[root@svr5 ~]# netstat -anptl | grep httpd //检查已无httpd监听
[root@svr5 ~]#
确认对未识别的控制参数的响应:
[root@svr5 ~]# service myhttpd status
用法: /etc/init.d/myhttpd {start|stop|restart}
4)添加myhttpd服务
使用chkconfig --add添加myhttpd服务,确认结果:
[root@svr5 ~]# chkconfig --add myhttpd
[root@svr5 ~]# chkconfig --list myhttpd
myhttpd 0:关闭 1:关闭 2:关闭 3:关闭 4:关闭 5:关闭 6:关闭
如果已经有其他关于httpd服务的自启脚本,这里建议就不要将myhttpd也设为自动开启了,否则会出现端口冲突等故障。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: