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?

  • 5
    If the o option is defined to accept an argument (with a : following it in the option specification), the above should work. Can you post the excerpt of your code that is giving you trouble? May 31 '13 at 11:25
  • Aha! The o option did not have a : in the optspec. Adding the : and it worked straight away! Add that as an answer and I'll accept it.
    – batfastad
    May 31 '13 at 13:07
  • For a easy workaround, just pass no-create-db replace and prefix with -- before pass to mysqldump.
    – favadi
    May 10 '14 at 4:47

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:

       while getopts ab: name
           case $name in
           a)    aflag=1;;
           b)    bflag=1
           ?)   printf "Usage: %s: [-a] [-b value] args\n" $0
                 exit 2;;
       if [ ! -z "$aflag" ]; then
           printf "Option -a specified\n"
       if [ ! -z "$bflag" ]; then
           printf 'Option -b "%s" specified\n' "$bval"
       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" \
  • I think it would skip off an opt you wanted to pass through. May 1 '18 at 19:04

One work-around would be:

... -o "x--no-create-db --replace"

# in the script:

As a general rule: You should quote everything (that may be quoted).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.