mysql - How to write a bash function to wrap another command? -
i trying write function wrapper mysql command
if .my.cnf exists in pwd, automatically attach --defaults-file=.my.cnf command
here's i'm trying
function mysql { if [ -e ".my.cnf" ]; /usr/local/bin/mysql --defaults-file=.my.cnf "$@" else /usr/local/bin/mysql "$@" fi } the idea is, want able use mysql command exactly before, only, if .my.cnf file present, attach argument
question: run trouble method? there better way it?
if specify --defaults-file=foo.cnf manually, should used instead of .my.cnf.
your function written fine. touch dryer:
function mysql { if [ -e ".my.cnf" ]; set -- --defaults-file=.my.cnf "$@" fi /usr/local/bin/mysql "$@" } that set command puts my.cnf argument @ beginning of command line arguments
only if option not present:
function mysql { if [[ -e ".my.cnf" && "$*" != *"--defaults-file"* ]]; set -- --defaults-file=.my.cnf "$@" fi /usr/local/bin/mysql "$@" }
Comments
Post a Comment