exit函数的作用是(exit(0)函数的作用是)

徐博客 80 0

#前言:今天我们来聊聊shell脚本中的函数知识,看一下函数的优势,执行过程和相关的使用案例。

#简介

1、函数也具有别名类似的功能2、函数是把程序里多次调用相同的代码部分定义成一份,然后给这份代码定义个名字,如果出现重复的就调用就行了

#函数的优势

1、把相同的程序段定义成函数,可以减少整个程序的代码量2、可以让程序代码结构更清晰3、增加程序的可读、易读性、以及管理性4、可以实现程序功能模块化,不同的程序使用函数模块化

#语法格式

函数名(){ 指令 return n}规范写法function 函数名(){ 指令 return n}#提示:shell的返回值是exit输出返回值,函数里用return输出返回值

#函数的执行

调用函数#1、直接执行函数名即可(不带括号)#注意执行函数时,函数后的小括号不要带了函数定义及函数整体必须在要执行的函数名的前面定义 #2、带参数的函数执行方法 函数名 参数1 参数2#提示:函数的传参和脚本的传参类似#shell的位置参数($1 $2 $3 $4 $5 $# $* $? $@)都可以时函数的参数#$0比较特殊,仍然是父脚本的名称#在shell函数里面,return命令功能与shell里的exit类似,作用时跳出函数#在shell函数里面使用exit会退出整个shell脚本,而不是退出shell函数#return语句会返回一个退出值(返回值)给调用函数的程序#使用

#例1:没有去调用函数

[root@shell scripts]# pwd/scripts[root@shell scripts]# cat hs01.sh #!/bin/bashguoke(){ echo "I am guoke"}[root@shell scripts]# sh hs01.sh [root@shell scripts]# #如果没有去调用函数的话,那么就没有输出

#例2:调用函数

[root@shell scripts]# cat hs01.sh #!/bin/bashguoke(){ echo "I am guoke"}guoke #调用函数[root@shell scripts]# sh hs01.sh I am guoke

#例3:多次调用

[root@shell scripts]# cat hs01.sh #!/bin/bashguoke(){ echo "I am guoke"}guokeguokeguoke[root@shell scripts]# sh hs01.sh I am guokeI am guokeI am guoke

#例4:将函数写到/etc/init.d/functions里面,然后通过其他脚本进行调用

#/etc/init.d/functionsboy(){ echo "I am guoke-boy"}return 0#提示:不要放在return 0后面,要不然就是退出了,没有调用[root@shell scripts]# cat hs01.sh #通过脚本去调用boy函数#!/bin/bash. /etc/init.d/functions #引入系统函数库guoke(){ echo "I am guoke"}guokeboy #调用/etc/init.d/functions中的函数[root@shell scripts]# sh hs01.sh #执行之后打印I am guokeI am guoke-boy

#例5:将函数写到/etc/init.d/functions里面,通过其他脚本进行调用然后传参

#/etc/init.d/functionsboy(){ echo "I am $1"}#提示:$1:脚本的传入的第一个参数[root@shell scripts]# cat hs01.sh #通过脚本去调用boy函数#!/bin/bash. /etc/init.d/functions #引入系统函数库guoke(){ echo "I am guoke"}guokeboy guoke-boy #调用/etc/init.d/functions中的函数,后面接着传参[root@shell scripts]# sh hs01.sh #执行之后打印I am guokeI am guoke-boy

#例6:设置提示函数,如果传的参数的值不符合就打印帮助函数

[root@shell scripts]# cat hs02.sh #!/bin/bashusage(){ echo "Usage:$0 key beginservernum endservernumexample:$0 ff 1 2"}[[ $# != 3 ]] && usage && exit 1 #如果传入的参数不等于3的话,就调用后面的函数,并退出脚本[[ -z $1 || -z $2 || -z $3 ]] && usage && exit 1 #如果传入的$1,$2,$3三个参数的值为空,那么就调用后面的函数,并退出脚本[root@shell scripts]# sh hs02.sh 22 33 #当传入的参数不等于3个的时候就执行usage函数,并退出脚本Usage:hs02.sh key beginservernum endservernumexample:hs02.sh ff 1 2

#例7:将函数的传参转换成脚本文件命令行传参,判断任意指定的URL是否存在异常

[root@shell scripts]# cat hs03.sh #!/bin/bash. /etc/init.d/functionsfunction usage(){ echo $"usage:$0 url" exit 1}function check_url(){ wget --spider -q -o /dev/null --tries=1 -T 5 $1 if [ $? -eq 0 ];then action "$1 is success." /bin/true else action "$1 is failure." /bin/false fi}function main(){ if [ $# -ne 1 ];then usage fi check_url $1}main $*#参数解释

. /etc/init.d/functions #引入系统函数库function usage(){ #帮助函数function check_url(){ #检测URL函数 wget --spider -q -o /dev/null --tries=1 -T 5 $1 #--spider:判断网址是否有效,-q:不显示执行过程,-o:将软件输出信息保存到软件,-T:指定超时时间 action "$1 is success." /bin/true #action:调用系统函数库的用法function main(){ #主函数 if [ $# -ne 1 ];then #判断:如果传参的参数不等1个,那么久打印帮助函数,提示用户 check_url $1 #接收函数的传输main $* #$*:把命令行接收的所有参数作为函数参数传给函数内部

#测试

[root@shell scripts]# sh hs03.sh #如果没有加参数,就调用提示函数usage:hs03.sh url[root@shell scripts]# sh hs03.sh ]

#例8:给任意字符串加指定颜色

[root@shell scripts]# cat hs04.sh #!/bin/bashRED_COLOR='\E[1;31m'GREEN_COLOR='\E[1;32m'YELLOW_COLOR='\E[1;33m'BLUE_COLOR='\E[1;34m'PINK='\E[1;35m'RES='\E[0m'usage(){ if [ $# -ne 2 ];then echo "USAGE:$0 {red|green|yellow|blue|pink}" contents exit 1 fi}color(){ case "$1" in red) echo -e "${RED_COLOR} $2 ${RES}" green) echo -e "${GREEN_COLOR} $2 ${RES}" yellow) echo -e "${YELLOW_COLOR} $2 ${RES}" blue) echo -e "${BLUE_COLOR} $2 ${RES}" *) usage esac}main(){ if [ $# -ne 2 ];then usage fi color $1 $2}main #参数解释

#1.定义颜色变量 数字对应的颜色:30(黑色)、31(红色)、32(绿色)、33(黄色)、34(蓝色、35(粉红)、36(青色)、37(白色)#2.定义帮助函数#3.定义颜色函数,使用case来获取输入的值#4.主函数,判断输入的参数是否为2个,如果不是就调用帮助函数 #测试

#如果执行脚本,不加参数的话就打印帮助函数

exit函数的作用是(exit(0)函数的作用是)-第1张图片

#例9:使用shell函数开发rsync服务启动脚本

#使用start、stop、restart函数将代码 模块化,使用系统函数action优化显示

[root@shell init.d]# cat rsyncd #!/bin/bash#chkconfig: 2345 20 80#description: Rsyncd start scripts by guoke.. /etc/init.d/functionsfunction usage(){ echo $"usage:$0 {start|stop|restart}" exit 1}function start(){ rsync --daemon sleep 1 if [ `netstat -unplt | grep rsync | wc -l` -ge 1 ];then    action "rsyncd is started." /bin/true else    action "rsyncd is started." /bin/false fi}function stop(){ killall rsync &/dev/null sleep 2 if [ `netstat -unptl | grep rsync |wc -l` -eq 0 ];then    action "rsyncd is stopped." /bin/true else    action "rsyncd is stopped." /bin/false fi}function restart(){ stop sleep 2 start}function main(){ if [ $# -ne 1 ];then usage fi case "$1" in start) start stop) stop restart) stop sleep 1 start *) usage esac}main $#参数解释:引入系统函数库,定义帮助函数,然后定义start函数,stop函数,restart函数,定义主函数,主函数里面首先使用if判断传入的参数是不是为一个,如果不是就调用帮助函数,然后使用case语句获取传入的参数,再调用相关的函数,$*:把命令行接收的所有参数作为函数参数传给函数内部

#测试

[root@shell init.d]# sh rsyncd stoprsyncd is stopped. [ OK ][root@shell init.d]# sh rsyncd startrsyncd is started. [ OK ][root@shell init.d]# sh rsyncd restartrsyncd is stopped. [ OK ]rsyncd is started. [ OK ]

#总结:将脚本中功能进行模块化之后,就会使脚本比较易读和清晰,提升管理效率。

抱歉,评论功能暂时关闭!