我想把我的getopt调用放到一个函数中,这样我就可以让我的脚本更整洁一点。我已经读了一些Using getopts inside a Bash function指南,但它们似乎是针对getopts而不是getopt的,并且无法理解它。
我在脚本的开头有下面的getopt调用
#-------------------------------------------------------------------------------
# Main
#-------------------------------------------------------------------------------
getopt_results=$( getopt -s bash -o e:h --long ENVIRONMENT:,HELP:: -- "$@" )
if test $? != 0
then
echo "Failed to parse command line unrecognized option" >&2
Usage
exit 1
fi
eval set -- "$getopt_results"
while true
do
case "$1" in
-e | --ENVIRONMENT)
ENVIRONMENT="$2"
if [ ! -f "../properties/static/build_static.${ENVIRONMENT}.properties" -o ! -f "../properties/dynamic/build_dynamic.${ENVIRONMENT}.properties" ]; then
echo "ERROR: Unable to open properties file for ${ENVIRONMENT}"
echo "Please check they exist or supply a Correct Environment name"
Usage
exit 1
else
declare -A props
readpropsfile "../properties/dynamic/dynamic.${ENVIRONMENT}.properties"
readpropsfile "../properties/static/static.${ENVIRONMENT}.properties"
fi
shift 2
;;
-h | --HELP)
Usage
exit 1
;;
--)
shift
break
;;
*)
echo "$0: unparseable option $1"
Usage
exit 1
;;
esac
done当我把所有的东西放入函数中时,比如调用parse_command_line (),然后用parse_command_line "$@"调用它,我的脚本就会死掉,因为它不能计算出调用时所用的参数。根据一些指南,我已经尝试使OPTIND本地化。有什么建议吗?谢谢。
https://stackoverflow.com/questions/47639468
复制相似问题