Bash:Getopts从未运行内核循环[处于保留状态]

问题描述 投票:0回答:1

[当它刚停止一起运行时,我开始理解getopts。当我在小型个人测试中尝试使用getopts时,它可以完美运行。我一直盯着屏幕看了两天,现在出了什么问题,我什至使用了一个shellchecker,它说一切正常。我试图在getopts while循环内回显,但从未打印过,更不用说在情况下的回显了。这基本上就是我的代码,

helpmenu(){
   case $1 in
      general)
         echo "Usage, some operationtypes, -h or --help idenifier explained"
         ;;
      eachoperationtype)
         echo "Explain this operationtype"
         ;;

}
operationtype=$1
if [[ operationtype == "Operation1" ]]; then

   # Setting some variables to default values here
   var1=0 #Default values
   var2=4
   var3=5
   ...

   echo "this prints" # this prints

   while getopts ":hs:d:m:f:a" opt; do

           echo "does not print" # this does not print

           case ${opt} in
                   h )
                       helpmenu operationtype #Simple function that prints out script usage
                       ;;
                   s )
                       var=$OPTARG
                       ;;
                d )
                       var2=$OPTARG
                       ;;       
                m )
                       var3=$OPTARG
                       ;;
                f )
                       var4=$OPTARG
                       ;;
                a )
                       bool=true
                       ;; 
                \? )
                    echo "Syntax error: Unrecognised flags given"
                    helpmenu operationtype
                    ;;
                : )
                    echo "Syntax error: Invalid number of arguments"
                    helpmenu general # No arguments given - shows help menu with operationtypes so they can ./Script operationtype -h
                    ;;
        esac
   done 
   shift $((OPTIND -1))

   # Do some other stuff, works fine

#Other operations have similar structure to operationtype1
elif [[ operationtype == "operation2" ]]; then
   # Similar program, potentially different flags, function, meaning etc
fi

脚本的要点是接受初始的第一个参数,它是所需的操作类型,并在初始getopts之后在不同的elifs中运行不同的if。每个参数都会接受特定数量的参数以及一系列可选标志。也就是说,您可以有一个脚本执行./Script greetings "hello" -t 3,例如可以执行echo "hello" 3次,而在同一脚本./Script age -u User1中具有另一个函数,该函数可以在列表中提供user1的使用期限。我要处理的另一个问题是所需变量的设置,所需变量通常应正常收集而没有标志。问题是我希望针对每种操作类型弹出一个帮助菜单(快速使用)-即./Script operation1 -h返回operation1的帮助菜单,但是我不允许这种语法,因为在尝试设置所需的语法时它将返回错误论点。另外,我还需要确保为每种操作类型放置正确数量的必需参数,因为其他任何数量都会导致错误。感谢您事先提供的帮助。

编辑:我已经使用shellcheck.net检查了我的代码,并且我相信没有基于语法的错误。我并不需要真正的代码细节方面的帮助,而是我上面所强调的粗略想法,是获得所需结果的最佳方法是什么。这是解决此问题的最佳方法吗?如何获得基于operationtypehelpmenu功能?为什么getopts没有运行?抱歉,如果您有任何混淆,我将删除pastebin链接,因为这根本不是我的意图。

bash function syntax getopts
1个回答
1
投票

实施中存在多个问题。很难说这是THE问题。在查找逻辑错误之前,请考虑先修复所有语法错误:

  • 缺少尾随';;'在heplmenu中的echo "Usage, some operationtypes
  • 缺少尾随';;'在echo "Explain this operationtype" ;
  • 缺少'esac'来终止帮助菜单中的案例
  • if [[ operationtype -eq "Operation1" ]]中使用'-ep'代替'=='>
  • 使用数字'-eq',而不是[[ operationtype -eq "operation2" ]]中的'=='>
  • [首要目标应该是通过'bash -n script.sh'获得通行证-这将使您获得清晰的语法。第二个目标是使用'bash -x script.sh args'运行脚本,并查找逻辑错误。

© www.soinside.com 2019 - 2024. All rights reserved.