在非选项参数之后使用getopts解析选项


10

我在文件中有一个bash脚本,如下所示nepleaks_upd.sh,我希望以此运行./nepleaks_upd.sh bootstrap --branch off。无法接受--branch,但是它的作用是./nepleaks_upd.sh bootstrap -b off

usage() { echo "Usage: $0 [prepare | up | down]  [-b <on/off>]" 1>&2; exit 1; }                                                                                                 

case "$1" in                                                                                           
               bootstrap)                                                                                  
                  while getopts ":b:" o; do                                                            
                   case "${o}" in                                                                      
                    b)                                                                                 
                        b=${OPTARG}                                                                    
                        if [ ${b} == "off" ]; then                                                      
                               echo "git clone https://github.com/iPrayag/dotfiles.git"  
                               ## logic                                                      
                        fi                                                                             
                        ;;                                                                             
                    *)                                                                                 
                        echo ${o}                                                                      
                        usage                                                                          
                        ;;                                                                             
                   esac                                                                                
                  done                                                                                 
                  shift $((OPTIND-1))                                                                  
                  echo "option1 = ${o}" 
                  echo "option2 = ${b}"                                                                      
                  if [ -z "${b}" ]; then                                                               
                         usage                                                                         
                  fi                                                                                   

                    ;;                                                                                 
                up)                                                                                         
                     echo "up"     
                     ##logic                                                                    
                     ;;                                                                                
                down)                                                                                  
                     echo "down" 
                     ##logic                                                                     
                    ;;                                                                                 
                *)                                                                                     
                    echo "Usage: $0 {up | down} dev"                                                
                    exit 1                                                                             
                    ;;                                                                                 
esac         

没有first case .. in .... esac,它就可以正常工作。随着case ... in ... esac,它给出了空白选项-b

$ ./nepleaks_upd.sh bootstrap -b off
option1 = ?
option2 = 
Usage: ./nepleaks_upd.sh [bootstrap | up | down]  [-b <on/off>]

使用之前,您不应该检查选项$1吗?
unxnut14年

@unxnut它唱samething兄弟
prayagupd

“没有第一case .. in .... esac,就可以正常工作” 是什么意思?您是说第5-25行./nepleaks_upd.sh -b off正确解析吗?也许您需要一个shift,所以getopts不要在“ bootstrap” 上窒息。
斯科特

@Scott是的,这部分工作正常。
祈祷

Answers:


12

getopts在第一个参数处开始解析,在第一个非选项参数处停止解析。这是标准惯例-一些GNU工具接受参数后的选择,但正常的事情是,在somecommand foo -bar qux-bar不会被解析为一个选项。

如果要在之后开始解析选项bootstrap,则需要指出。getopts使用OPTIND变量来记住它所处的位置。OPTIND从值1开始。要跳过第一个参数,请将其设置为2。

case "$1" in
   bootstrap)
      OPTIND=2
      while getopts ":b:" o; do
          

或者,您可以shift处理已经处理过的参数。

subcommand=$1; shift
case "$subcommand" in
    bootstrap)
      while getopts ":b:" o; do
          
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.