unix – 如何添加一个帮助方法到shell脚本?
发布时间:2020-12-15 16:28:07 所属栏目:安全 来源:网络整理
导读:如何检查-h属性是否已传递到shell脚本中?我想显示一个帮助消息,当用户调用myscript.sh -h。 这里是一个bash的例子: usage="$(basename "$0") [-h] [-s n] -- program to calculate the answer to life,the universe and everythingwhere: -h show this he
如何检查-h属性是否已传递到shell脚本中?我想显示一个帮助消息,当用户调用myscript.sh -h。
这里是一个bash的例子:
usage="$(basename "$0") [-h] [-s n] -- program to calculate the answer to life,the universe and everything where: -h show this help text -s set the seed value (default: 42)" seed=42 while getopts ':hs:' option; do case "$option" in h) echo "$usage" exit ;; s) seed=$OPTARG ;; :) printf "missing argument for -%sn" "$OPTARG" >&2 echo "$usage" >&2 exit 1 ;; ?) printf "illegal option: -%sn" "$OPTARG" >&2 echo "$usage" >&2 exit 1 ;; esac done shift $((OPTIND - 1)) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |