1

Wrapping another command's parameters

I have a command tool1 that parses arguments this way:

#!/usr/bin/env bash
# ...
while [[ $# -ge 1 ]]
do
        key="$1"
        case $key in
                -o|--option)
                OPT="$2"
                shift
                ;;  

                -u|--user)
                USR="$2"
                shift
                ;; 

                -*) 
                echo -e "Unrecognized option: \"$key\"" && exit 1 
                ;;  

                *)  
                OTHERS+=("$1")
                ;;  
        esac
        shift
done
# ...

I have tool2 that calls tool1. Thus tool2 will have to pass parameters to tool1. It may also need to process the same parameters (--user)

tool2 looks like:

#!/usr/bin/env bash
# ...
while [[ $# -ge 1 ]]
do
        key="$1"
        case $key in
                -O|--option2)
                opt2="$2"
                shift
                ;;  

                -u|--user)
                USR="$2"
                OTHERS+=("-u $2")
                shift
                ;;  

                -*) 
                echo -e "Unrecognized option: \"$key\"" && exit 1 
                ;;  
                *)  
                OTHERS+=("$1")
                ;;  
        esac
        shift
done

## Call tool1 with other parameters to pass
bash tool1.sh ${OTHERS[@]}
# ...

To sum up --option2 is an option used only by tool2. --user is common to both tools, and may be used by tool2 too, before calling tool1.sh. Because of this, in this example --user has to be explicitly passed to tool1 thanks to the array OTHERS.

I'd like to know about possible and/or alternative ways of dealing with such parameter redundancies. A methodology that would help me wrapping another tool's expected parameters/options, without having to copy/paste the lines regarding the parsing of such redundant parameters/options.

5
  • Why don't you use getopt builtin ? Commented Apr 27, 2017 at 9:25
  • For portability issues. getopt isn't standardized. getopts however doesn't support long options. Commented Apr 27, 2017 at 9:43
  • What if an option is in the second script but not the first? Also if both scripts can act upon a parameter how is it redundant? Commented Apr 27, 2017 at 10:06
  • It's worth editing your question to clarify if tool1 is a stand-alone command that is also called outside of tool2. If tool1 is only called by tool2, I'd write it as a function within tool2. Commented Apr 27, 2017 at 11:12
  • @123 the parsing is redundant. My question is more a whim to do something elegant. Commented Apr 27, 2017 at 11:16

1 Answer 1

1

tool2's approach is fine. However, you aren't setting OTHERS correctly.

-u|--user)
  USR="$2"
  OTHERS+=("-u" "$2")
  shift

-u and its argument need to remain separate array elements, just as they were separate arguments to tool2. You also need to quote the expansion of OTHERS, to preserve arguments containing word-splitting characters or globs:

bash tool1.sh "${OTHERS[@]}"

Finally, all-uppercase variable names are reserved for use by the shell itself; don't define such names yourself. Just use others instead of OTHERS.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the insight. Capitalizing names for exported variables is ok though, right?
If by "capitalize", you mean Others, sure. Don't create any variables consisting solely of uppercase letters yourself, though. Whether or not you are exporting the variable is irrelevant.
OK cause in some official documentation I did read that declaring personal uppercase variable (OTHER) is relevant only if the said variables are exported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.