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

【Linux】shell---函数的简单例子

2016-06-01 17:27 363 查看
Shell脚本语言中也有函数功能,可以帮助我们简化很多代码。下面看一个例子。

创建一个文件,输入一下内容

#!/bin/bash
function printit()
{
echo -n "Your choice is $1"    #echo -n表示不输出换行符
}
function help()
{
cat<< HELP
echo "this is help manual"
HELP
}
echo "This program will print your selection !"
case $1 in
-h) help;;
"one") printit;echo $1 |tr 'a-z' 'A-Z';;   #将参数做大小写转换!
"two") printit;echo $1 |tr 'a-z' 'A-Z';;
"three") printit;echo $1 |tr 'a-z' 'A-Z';;
*) echo "Usage $0 {one|two|three}";;
esac


在这段代码中包含两个函数,一个是help()函数,一个是printit()函数,然后在case语句中调用这两个函数。

其中help()函数是打印帮助文档这个函数以cat<< HELP开头,以HELP结尾(结尾处的HELP必须顶头写,不能有空白字符)

运行一下试试

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