shell 编程 帮助功能的实现

一个程序,往往需要帮助说明. 激活帮助说明的方法介绍两种 1. 最简单的办法是通过参数个数判别.例如不带任何参数就显示帮助说明. 2. 正规做法是命令行后跟 -h 参数. shell 帮助的写法可以用一堆echo 指令向控制台输出,更好的做法是用 here doc 语法,说明文档排版整齐,跟输出一致. 没有一堆echo 及 双引号等. 下面给出实例: 1. 简单方法 #!/bin/bash - set -o nounset # Treat unset variables as an error help() { cat <<- EOF Desc: 该程序用来.... Usage: ./1.sh <filename> Author: hjjdebug License: ... EOF exit 0 } if [ $# -lt 1 ] # 不能用 < 这里是数值比较,不是字符串比较 then help fi 2. 正规方法 #!/bin/bash - #set -o nounset # 这个选项关闭吧,否则$1无定义它报语法错.影响视觉. help() { cat <<- EOF Desc: 该程序用来.... Usage: ./1.sh <filename> Author: hjjdebug License: ... EOF exit 0 } #这里通过判断$1是否存在判别,也可以通过$#判别,shift会改变两者的值,while [ -n "$1" ]; do case $1 in -h) help;; # function help is called --) shift;break;; # end of options -*) echo "error: no such option $1."; exit 1;; *) break;; esac done

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...