开发者

variables in awk

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜