I have a following line in my shell script.
time mysqlshow -u$user -p$password | egrep -v 'information_schema|performance_schema' | awk '{print "mysql -u$user -p$password -Bse \"drop database", $2, "\""}'
The first $user is correctly changing to username variable. But the one within awk statement does n开发者_C百科ot. I tried to add double and single quotes with and without escape characters \ but it does not work.
You need to make sure that $user
and $password
are surrounded by double quotes ("
) or by nothing and, as @jkerian mentioned, that $2
is surrounded by single quotes ('
) or, if it is in double quotes, preceded by a backslash to make \$2
.
Try this (this is the cleaner one in my opinion):
time mysqlshow -u$user -p$password |
egrep -v 'information_schema|performance_schema' |
awk '{print "mysql -u'"$user"' -p'"$password"' -Bse \"drop database", $2, "\""}'
Or this:
time mysqlshow -u$user -p$password |
egrep -v 'information_schema|performance_schema' |
awk '{print "mysql -u'$user' -p'$password' -Bse \"drop database", $2, "\""}'
Or this:
time mysqlshow -u$user -p$password |
egrep -v 'information_schema|performance_schema' |
awk "{print \"mysql -u$user -p$password -Bse \\\"drop database\", \$2, \"\\\"\"}"
It's a mess, I know. But it should work.
That $2 needs to be handed down to awk without being interpreted by the shell.
Surround the $2 with single-quotes.
精彩评论