首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BASH函数中的bash getopt

BASH函数中的bash getopt
EN

Stack Overflow用户
提问于 2017-12-05 02:13:09
回答 0查看 1.7K关注 0票数 1

我想把我的getopt调用放到一个函数中,这样我就可以让我的脚本更整洁一点。我已经读了一些Using getopts inside a Bash function指南,但它们似乎是针对getopts而不是getopt的,并且无法理解它。

我在脚本的开头有下面的getopt调用

代码语言:javascript
复制
#-------------------------------------------------------------------------------
# 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本地化。有什么建议吗?谢谢。

EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47639468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档