bash argument case for args in $@

后端 未结 5 1817
无人共我
无人共我 2021-02-02 12:14

I have a script with a long list of OPTIONAL arguments. some have associated values.

Such as:

.script --first 2012-12-25 --last 2012-12-26 --copy --remov         


        
5条回答
  •  轮回少年
    2021-02-02 12:22

    getopts cannot have optional arguments it seems. otherwise great.

    my solution

    loop the $@ and setting a variable equal to x=$arg do the case switch on that variable (rather than arg)

    that worked fine for arguments of the type --startdate 2012-12-25 --enddate 2012-12-29

    but did not work for --remove that has no following argument.

    therefore tack on stuff (unlikely argument) onto the arg string.

    leaving the following

    argc="$@ jabberwhocky" 
    echo $argc
    x=0
    # x=0 for unset variable
    for arg in $argc
    do
       case $x in
            "--start" )
              STARTDATE=$arg ;;
            "--end" )
              ENDDATE=$arg ;;
            "--copy" )
              COPY=true;;
            "--remove" )
              REMOVE=true;;
    

    ... and so on....

        esac
        x=$arg
    done
    

提交回复
热议问题