Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux shell getopts参数集合问题_Linux_Bash_Shell_Getopts - Fatal编程技术网

Linux shell getopts参数集合问题

Linux shell getopts参数集合问题,linux,bash,shell,getopts,Linux,Bash,Shell,Getopts,我的shell脚本中有以下代码 show_help() { cat <<EOF Usage: ${0##*/} [-h help] [-g GATEWAY_HOSTID] [-t TIMEZONE] -h display this help and exit -g GATEWAY_HOSTID zabbix gateway identifier (e.g. '20225')

我的shell脚本中有以下代码

show_help()
{
    cat <<EOF
Usage: ${0##*/} [-h help] [-g GATEWAY_HOSTID] [-t TIMEZONE]

         -h                     display this help and exit
         -g GATEWAY_HOSTID      zabbix gateway identifier (e.g. '20225')
         -t Time Zone          TimeZone against which you want to test
EOF
}

OPTIND=1

while getopts "g:h:t" opt; do
  case "$opt" in
    h)
      show_help
      exit 0
      ;;
    g)
      gateway_hostid=$OPTARG
      ;;
    t)
      timezone=$OPTARG
      ;;
    esac
done

shift $((OPTIND-1))

if [[ ! $timezone ]]; then
    timezone="UTC"
fi

if [[ ! $gateway_hostid ]]; then
    echo "hostid is missing!!! Exiting now."
    exit
fi

您的问题是optstring。您正在指定
h:
,这意味着
-h
需要一个选项。您还指定了不带
t
,这意味着
t
不需要选项


拥有
g
t
选项和
h
不需要选项的选项字符串是
hg:t:
您的问题在于选项字符串。您正在指定
h:
,这意味着
-h
需要一个选项。您还指定了不带
t
,这意味着
t
不需要选项


拥有
g
t
选择选项和
h
不需要的选项字符串是
hg:t:

您可能正在寻找
getopts“g:ht:
。您可能正在寻找
getopts”g:ht:
./script_name.sh -g 20225 -t Europe/Zurich
./script_name.sh -g 20225 -t CEST