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 inman getopts
: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 ofgetopts
processed arguments areshift
ed away and the remaining are printed out all at once as$*
. If you handle it similarly, you could make the following work:And to call the wrapped application: