Storing bash script argument with multiple values

后端 未结 1 419
情歌与酒
情歌与酒 2021-01-25 08:38

I would like to be able to parse an input to a bash shell script that looks like the following.

myscript.sh --casename obstacle1 --output en --variables v P pRes         


        
1条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-25 09:11

    One conventional practice (if you're going to do this) is to shift multiple arguments off. That is:

    variables=( )
    case $key in
      --variables)
        while (( "$#" >= 2 )) && ! [[ $2 = --* ]]; do
          variables+=( "$2" )
          shift
        done
        ;;
    esac
    

    That said, it's more common to build your calling convention so a caller would pass one -V or --variable argument per following variable -- that is, something like:

    myscript --casename obstacle1 --output en -V=v -V=p -V=pResidualTT
    

    ...in which case you only need:

    case $key in
      -V=*|--variable=*) variables+=( "${1#*=}" );;
      -V|--variable)   variables+=( "$2" ); shift;;
    esac
    

    0 讨论(0)
提交回复
热议问题