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

来源:互联网 发布:围棋训练软件 编辑:程序博客网 时间:2024/05/18 22:44

Shell脚本语言中也有函数功能,可以帮助我们简化很多代码。下面看一个例子。
创建一个文件,输入一下内容

#!/bin/bashfunction 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必须顶头写,不能有空白字符)
运行一下试试
这里写图片描述

0 0