Bash – shell script options pass through to sub-command

argumentsbashgetopts

I'm writing a wrapper script for that uses getopts to set various options for mysqldump. It's working well so far.

However one thing I would like to do is be able to add another option in my shell script, the contents of which should be passed to the mysqldump command exactly. Sort of like an options pass through like this:
./mysqldumpwrapper.sh -u username -p password -h localhost -o "--no-create-db --replace"
with the string content of the -o argument passed exactly through to my mysqldump command.

Because I'm using getopts when trying the above I get the following output:
Invalid option: --

Does anyone know how to work around this?
Is there a way to escape the entry of options so that getopts won't parse the contents as another option?

Then when it comes to triggering the actual command from my shell script, this is what I've got:
mysqldump "$OPTIONSPASSTHROUGH" --host=$MYSQL_HOST --user=$MYSQL_USER --password=$MYSQL_PASS "$DB" > "$FILE_DEST"
Because there may well be spaces in the OPTIONSPASSTHROUGH variable, I have quoted that variable in that command. Is that correct?

Best Answer

If you specify that -- marks the end of options and all following arguments will be passed through to the subcommand, you can do something like you might find in man getopts:

       aflag=
       bflag=
       while getopts ab: name
       do
           case $name in
           a)    aflag=1;;
           b)    bflag=1
                 bval="$OPTARG";;
           ?)   printf "Usage: %s: [-a] [-b value] args\n" $0
                 exit 2;;
           esac
       done
       if [ ! -z "$aflag" ]; then
           printf "Option -a specified\n"
       fi
       if [ ! -z "$bflag" ]; then
           printf 'Option -b "%s" specified\n' "$bval"
       fi
       shift $(($OPTIND - 1))
       printf "Remaining arguments are: %s\n$*"

Specifically I'm referring to the very end there - getopts stops processing options when it encounters -- so all of those arguments will remain in $@. In the example above all of getopts processed arguments are shifted away and the remaining are printed out all at once as $*. If you handle it similarly, you could make the following work:

/mysqldumpwrapper.sh \
    -u username \
    -p password \
    -h localhost \
    -- \
    -now --all of these --are passed through

And to call the wrapped application:

mysqldump "$@" \
    --host=$MYSQL_HOST \
    --user=$MYSQL_USER \
    --password=$MYSQL_PASS "$DB" \
> "$FILE_DEST"
Related Question