Linux shell getopts参数集合问题
我的shell脚本中有以下代码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')
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