#前言:今天我们来聊聊shell脚本中的函数知识,看一下函数的优势,执行过程和相关的使用案例,我们也来看一下shell和python的函数书写方式有什么不同

#简介

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

#函数的优势

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

#语法格式

函数名(){
指令
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语句会返回一个退出值(返回值)给调用函数的程序

#我们来看一下python的函数书写方式

#提示:def是define的意思,定义

最基本的语法:
def 函数名():
函数体
   函数名() #调用函数 带有参数的语法
def 函数名(形参列表):
函数体(代码块,return) 函数名(实参列表) :调用

#看一下执行过程

# def wan():  #定义函数
# print("今天一起去玩")
# print("去哪里玩呢")
# print("我不知道")
# wan() #调用函数
'''讲解执行的过程
.定义函数wan()
.调用函数wan()
.准备开始执行函数
.打印,今天一起去玩
.打印,去哪里完
.打印,我不知道
.函数执行完毕,本次调用完毕,wan()函数调用完毕
'''

#使用

#例1:没有去调用函数

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

#例2:调用函数

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

#例3:多次调用

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

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

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

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

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

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

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

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

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

#参数解释

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

#测试

[root@shell scripts]# sh hs03.sh   #如果没有加参数,就调用提示函数
usage:hs03.sh url
[root@shell scripts]# sh hs03.sh www.guokeboy.com #输入错误地址
www.guokeboy.com is failure. [FAILED](失败)
[root@shell scripts]# sh hs03.sh www.baidu.com #输入正确地址
www.baidu.com is success. [ OK ]

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

[root@shell scripts]# cat hs04.sh
#!/bin/bash RED_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 ];then
echo "USAGE:$0 {red|green|yellow|blue|pink}" contents
exit
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 ];then
usage
fi
color $ $
}
main $*

#参数解释

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

#测试

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

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

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

[root@shell init.d]# cat rsyncd
#!/bin/bash
#chkconfig:
#description: Rsyncd start scripts by guoke. . /etc/init.d/functions function usage(){
echo $"usage:$0 {start|stop|restart}"
exit
} function start(){
rsync --daemon
sleep
if [ `netstat -unplt | grep rsync | wc -l` -ge ];then
   action "rsyncd is started." /bin/true
else
   action "rsyncd is started." /bin/false
fi
} function stop(){
killall rsync &>/dev/null
sleep
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 ];then
usage
fi
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep
start
;;
*)
usage esac
}
main $*

#参数解释:引入系统函数库,定义帮助函数,然后定义start函数,stop函数,restart函数,定义主函数,主函数里面首先使用if判断传入的参数是不是为一个,如果不是就调用帮助函数,然后使用case语句获取传入的参数,再调用相关的函数,$*:把命令行接收的所有参数作为函数参数传给函数内部

#测试

[root@shell init.d]# sh rsyncd stop
rsyncd is stopped. [ OK ]
[root@shell init.d]# sh rsyncd start
rsyncd is started. [ OK ]
[root@shell init.d]# sh rsyncd restart
rsyncd is stopped. [ OK ]
rsyncd is started. [ OK ]

#总结:将脚本中功能进行模块化之后,就会使脚本比较易读和清晰,提升管理效率。好了,这次就分享到这了,写的不好地方还望指出,多多交流提高,下次再会。。。

												

最新文章

  1. Electron 不完全快速手册
  2. Atititcmd cli环境变量的调用设置与使用
  3. Bootstrap系列 -- 17. 复选框checkbox和单选择按钮radio
  4. 全文检索- Oracle/MySql/达梦
  5. VS2013 单元测试(使用VS2013自带的单元测试)
  6. java开发规范总结_代码编码规范
  7. UESTC_棋盘游戏 CDOJ 578
  8. JS复习:第七章
  9. Netty ByteBuf源码分析
  10. Spring REST API + OAuth2 + AngularJS
  11. 2017 年终总结 & 2018 年度计划
  12. ACM:读入优化
  13. openstack-mitaka部署
  14. 基于keepalived搭建MySQL热机集群
  15. 我的WafBypass之道(SQL注入篇)
  16. 最后一个单词的长度的golang实现
  17. ASP.NET MVC下载excel文档
  18. [JOI2017春季合宿]Port Facility[set、二分图]
  19. MYSQL常见的可优化点
  20. bzoj 3252: 攻略 -- 长链剖分+贪心

热门文章

  1. 通过ELK快速搭建集中化日志平台
  2. Spark OFF_HEP变迁
  3. Codeforces Round #612 (Div. 2)C. Garland
  4. JDBC阶段总结
  5. Docker Compose 项目打包部署
  6. UBB代码
  7. fsLayuiPlugin多数据表格使用
  8. 2017、2018面试分享(js面试题记录)记得点赞分享哦;让更多的人看到~~
  9. HBuilder-X 关闭eslint-vue 插件语法检查
  10. 报错: raise ImproperlyConfigured('mysqlclient 1.3.13 or newer is required; you have %s.' % Database.__version__)