awk中的变量

发布于 2024-12-08 22:14:49 字数 298 浏览 0 评论 0原文

我的 shell 脚本中有以下一行。

time mysqlshow -u$user -p$password |  egrep -v 'information_schema|performance_schema' | awk '{print "mysql -u$user -p$password -Bse \"drop database", $2, "\""}'

第一个 $user 正确更改为 username 变量。但 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 not. I tried to add double and single quotes with and without escape characters \ but it does not work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

安人多梦 2024-12-15 22:14:49

您需要确保 $user$password 用双引号 (") 或什么都不括起来,正如@jkerian提到的$2 用单引号 (') 括起来,或者如果用双引号括起来,则前面加一个反斜杠以形成 \$2

试试这个 。 (我认为这是更干净的):

time mysqlshow -u$user -p$password |
    egrep -v 'information_schema|performance_schema' |
    awk '{print "mysql -u'"$user"' -p'"$password"' -Bse \"drop database", $2, "\""}'

或者这个:

time mysqlshow -u$user -p$password |
    egrep -v 'information_schema|performance_schema' |
    awk '{print "mysql -u'$user' -p'$password' -Bse \"drop database", $2, "\""}'

或者这个:

time mysqlshow -u$user -p$password |
    egrep -v 'information_schema|performance_schema' |
    awk "{print \"mysql -u$user -p$password -Bse \\\"drop database\", \$2, \"\\\"\"}"

这是一团糟,我知道,但它应该有效。

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.

无人问我粥可暖 2024-12-15 22:14:49

$2 需要传递给 awk,而不由 shell 解释。

将 $2 用单引号引起来。

That $2 needs to be handed down to awk without being interpreted by the shell.

Surround the $2 with single-quotes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文