Answers:
这是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 -%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))
要在函数中使用此函数:
"$FUNCNAME"代替$(basename "$0")local OPTIND OPTARG通话前添加getoptsbasename前导破折号。
                    "$FUNCNAME"不能使用"$0"。此外,请添加local OPTIND OPTARG
                    FUNCNAME作品。我将所有函数都放在一个文件中,因此非常适合将它们扩展为对其他人有用的东西。
                    "$usage" 每个使用它的地方。
                    shift $((OPTIND - 1))用
                    Shell脚本的第一个参数可以作为变量使用$1,因此最简单的实现是
if [ "$1" == "-h" ]; then
  echo "Usage: `basename $0` [somestuff]"
  exit 0
fi
但是阿努巴瓦怎么说。
[它是POSIX兼容的版本。
                    function:如果您不想在运行函数后终止shell,则应替换exit 0为return。(我已在before之前完成过)
                    这是我用来启动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
     ;;
esac
我将启动停止重新启动放在单独的情况下有点奇怪,但它应该可以工作
if如果您只有一个选项要检查,并且始终是第一个选项($1),那么最简单的解决方案是if带有测试([)。例如:
if [ "$1" == "-h" ] ; then
    echo "Usage: `basename $0` [-h]"
    exit 0
fi
请注意,对于posix兼容性=将同样有效==。
$1?$1需要用引号引起来的原因是,如果没有引号,则$1外壳程序将尝试运行if [  == "-h" ]并失败,因为==在期望两个参数时仅给出了一个参数:
$ [ == "-h" ]
bash: [: ==: unary operator expected
getopt或getopts正如其他人所建议的 那样 ,如果您有一个以上的简单选项,或者需要您的选项来接受参数,那么您绝对应该使用来增加额外的复杂性。getopts
作为快速参考,我喜欢60秒的getopts教程。†
您可能还需要考虑使用该getopt程序而不是内置的shell getopts。它允许使用长选项,并在非选项参数之后使用选项(例如,foo a b c --verbose而不只是foo -v a b c)。这个Stackoverflow答案说明了如何使用GNU getopt。
† jeffbyrnes提到原始链接已终止,但值得庆幸的是,回机已将其存档。
最好使用bash的getopt工具。请查看此问答以获取更多帮助:在bash shell脚本中使用getopts获取长和短命令行选项