如何向shell脚本添加帮助方法?

时间:2011-03-29 15:06:51

标签: unix shell

如何检查-h属性是否已传入shell脚本?我想在用户拨打myscript.sh -h时显示帮助消息。

8 个答案:

答案 0 :(得分:138)

这是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
  • 之前添加getopts

答案 1 :(得分:39)

shell脚本的第一个参数可用作变量$1,因此最简单的实现将是

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

但是阿努巴瓦说的话。

答案 2 :(得分:21)

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

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="always" />

    <item
        android:id="@+id/contact"
        android:icon="@drawable/ic_star"
        android:orderInCategory="2000"
        android:title="@string/Rate"
        app:showAsAction="always" />
</menu>

我把启动停止重启放在一个单独的情况下有点奇怪但它应该可行

答案 3 :(得分:13)

如果您只有一个选项可供检查,它将始终是第一个选项($1),那么最简单的选项是带有测试if)的[。例如:

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

请注意,对于posix兼容性=将与==一样有用。

$1需要用引号括起来的原因是,如果没有$1,那么shell会尝试运行if [ == "-h" ]并失败,因为==只有当它期待两个时被给出一个论点:

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

作为suggested by others,如果您有多个简单的选项,或者需要您的选项来接受参数,那么您一定要考虑使用{的额外复杂性{1}}。作为快速参考,我喜欢The 60 second getopts tutorial

您可能还需要考虑getopt程序而不是内置shell getopts。它允许在非选项参数之后使用长选项和选项(例如getopts而不仅仅是foo a b c -v)。 This Stackoverflow answer解释了如何使用GNU foo -v a b c

jeffbyrnes提到original link已经死亡,但幸运的是way back machine已将其归档。

答案 4 :(得分:5)

最好使用bash的getopt工具。请查看此Q&amp; A获取更多帮助:Using getopts in bash shell script to get long and short command line options

答案 5 :(得分:0)

有一种无需 getoptgetopts 即可实现的简单方法:

display_help() {
    # taken from https://stackoverflow.com/users/4307337/vincent-stans
    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
}

log() {
    echo "This is a log"
}
while [[ "$#" -gt 0 ]]; do
    case $1 in
        -h|--help) display_help; shift ;;
        -l|--log) log; shift ;;
        # ... (same format for other required arguments)
        *) echo "Unknown parameter passed: $1" ;;
    esac
    shift
done

答案 6 :(得分:-1)

我认为你可以使用案例......

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

答案 7 :(得分:-2)

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

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