我如何添加一个帮助方法到一个shell脚本?

我如何检查一个-h属性是否已经被传入一个shell脚本? 我想在用户调用myscript.sh -h时显示帮助信息。

这里是bash的一个例子:

 usage="$(basename "$0") [-h] [-sn] -- 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 -%s\n" "$OPTARG" >&2 echo "$usage" >&2 exit 1 ;; \?) printf "illegal option: -%s\n" "$OPTARG" >&2 echo "$usage" >&2 exit 1 ;; esac done shift $((OPTIND - 1)) 

shell脚本的第一个参数是variables$1 ,所以最简单的实现是

 if [ "$1" == "-h" ]; then echo "Usage: `basename $0` [somestuff]" exit 0 fi 

但是什么anubhava说。

这里是我用它来启动一台vnc服务器的一部分

 #!/bin/bash start() { echo "Starting vnc server with $resolution on Display $display" #your execute command here mine is below #vncserver :$display -geometry $resolution } stop() { echo "Killing vncserver on display $display" #vncserver -kill :$display } ######################### # The command line help # ######################### display_help() { echo "Usage: $0 [option...] {start|stop|restart}" >&2 echo echo " -r, --resolution run with the given resolution WxH" echo " -d, --display Set on which display to host on " echo # echo some stuff here for the -a or --add-options exit 1 } ################################ # Check if parameters options # # are given on the commandline # ################################ while : do case "$1" in -r | --resolution) if [ $# -ne 0 ]; then resolution="$2" # You may want to check validity of $2 fi shift 2 ;; -h | --help) display_help # Call your function exit 0 ;; -d | --display) display="$2" shift 2 ;; -a | --add-options) # do something here call function # and write it in your help function display_help() shift 2 ;; --) # End of all options shift break ;; -*) echo "Error: Unknown option: $1" >&2 ## or call function display_help exit 1 ;; *) # No more options break ;; esac done ###################### # Check if parameter # # is set too execute # ###################### case "$1" in start) start # calling function start() ;; stop) stop # calling function stop() ;; restart) stop # calling function stop() start # calling function start() ;; *) # echo "Usage: $0 {start|stop|restart}" >&2 display_help exit 1 ;; 

有点奇怪,我把启动停止重新启动在一个单独的情况下,但它应该工作

如果你只有一个选项来检查,它将永远是第一个选项( $1 ),那么最简单的select是if有一个testing( [ )。 例如:

 if [ "$1" == "-h" ] ; then echo "Usage: `basename $0` [-h]" exit 0 fi 

请注意,对于posix兼容性=将工作以及==

$1需要被包含在引号中的原因是,如果没有$1那么shell会尝试运行if [ == "-h" ]并失败,因为==只有在期待两个时才被赋予一个参数:

 $ [ == "-h" ] bash: [: ==: unary operator expected 

正如其他人所build议的 那样 ,如果你不止一个简单的选项,或者需要你的选项来接受参数,那么你肯定会getopts额外的复杂性来使用getopts 。 作为一个快速参考,我喜欢60秒的getopts教程 。

您可能还想考虑getopt程序而不是内置的shell getopts 。 它允许非选项参数之后使用长选项和选项(例如foo abc -v而不仅仅是foo -vabc )。 这个Stackoverflow答案解释了如何使用GNU getopt

jeffbyrnes提到, 原来的链接死亡,但幸运的是, 回机器归档它。

最好使用bash的getopt设施。 请看这个问答以获取更多帮助: 在bash shell脚本中使用getopts来获取长短命令行选项

我认为你可以使用这种情况…

 case $1 in -h) echo $usage ;; h) echo $usage ;; help) echo $usage ;; esac 

我只是上传在线手册文件,并用curl检索它。 例如,下面的代码是从Github中检索。

  ####################################### # Show help # # Globals: # None # Arguments: # None # Returns: # None # ####################################### show_help () { curl https://raw.githubusercontent.com/KENJU/shellscript_todo/master/MANUAL | less }