Shell脚本中使用function(函数)示例
函数可以在shellscript当中做一个类似自定义执行命令,最大的功能就是可以简化我们很多的程序代码。需要注意的是shellscript的执行方式是由上而下/由左而右,因此在shellscript当中的function的设置一定要在程序的最前面,这样才能够在执行时被找到可用的程序段。
#!/bin/bash
#Program
# Thisprogramistoshowtheuseof"function"
#History
#2013/5/4byLvcyFirstrelease
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/loacl/sbin:~/bin
exportPATH
#输出统一信息
functionprintInfo()
{
echo-n"Yourchoiceis"
}
#将小写字符转换为大写字符
functiondotr()
{
tr'a-z''A-Z'
}
read-p"Pleaseinputyourchoice(one|two|three|four):"num
#用case做条件判断
case$numin
"one")
printInfo;echo$num|dotr
;;
"two")
printInfo;echo$num|dotr
;;
"Three")
printInfo;echo$num|dotr
;;
"four")printInfo;echo$num|dotr
;;
esac
exit0
下面是一个一般的带有function函数的shell脚本:
#!/bin/bash
#Program
# Thisprogramisshowtheparamsoffunction
#History
# 2013/5/14byLvcyFirstrelease
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
exportPATH
functionprintInfo()
{
echo"Yourchoiceis$1"
}
case$1in
"one")
printInfo1
;;
"two")
printInfo2
;;
"three")
printInfo3
;;
"four")
printInfo4
;;
esac
exit0
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志