bash adds escape chars when passing a string with quotes -


i'm trying run msyql command includes quoted string, eg:

mysql -h host -u=user -p=pass -e "show tables" database. 

here i'm having difficulty pass "show tables" quotes fuction executes command.

run_cmd()  # run command {   local output=$1   local timeout=$2   shift 2   ( $* > $output 2>&1 ) & # .... }  # query mysql   query_mysql(){     local mysql_cmd=( "mysql -h $host --user=$user --password=$pass -e "show tables" $db")     run_cmd $output $timeout "${mysql_cmd[@]}" } query_mysql 

i tried many combinations, command executed either without quotes or multiple single/double/escape chars. in cases final command becomes invalid due missing/additional quotes/chars.

few of attempts & final command:

"show tables" mysql -h localhost --user=root --password=foo -e show tables db1   'show tables' mysql -h localhost --user=root --password=foo -e ''\''show' 'tables'\''' db1  \"show tables\" mysql -h localhost --user=root --password=foo -e '"show' 'tables"' db1 

any suggestions pass quoted string is?

thanks in advance!

don't quote unrelated words inside array assignment. use

local mysql_cmd=( mysql -h "$host" --user="$user" --password="$pass" -e "show tables" "$db") 

instead of

local mysql_cmd=( "mysql -h $host --user=$user --password=$pass -e "show tables" $db") 

see difference between yours

$ mysql_cmd=( "mysql -h $host --user=$user --password=$pass -e "show tables" $db") $ printf %q\\n "${mysql_cmd[@]}" mysql\ -h\ host\ --user=user\ --password=pass\ -e\ show tables\ db 

and mine

$ mysql_cmd=( mysql -h "$host" --user="$user" --password="$pass" -e "show tables" "$db") $ printf %q\\n "${mysql_cmd[@]}" mysql -h host --user=user --password=pass -e show\ tables db 

also don't use unquoted $* when execute command. want use "$@" instead. so

( "$@" > $output 2>&1 ) & 

which doesn't need sub-shell () , can be

"$@" > $output 2>&1 & 

see http://mywiki.wooledge.org/bashfaq/050 more details why array command usage needs work way (and why having such trouble attempts).


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -