Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
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? –  Stéphane Chazelas 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. –  benbradley 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 at 4:47

2 Answers 2

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"
share|improve this answer

One work-around would be:

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

# in the script:
option="${option#x}"

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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