awk中的变量
我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要确保
$user
和$password
用双引号 ("
) 或什么都不括起来,正如@jkerian提到的,$2
用单引号 ('
) 括起来,或者如果用双引号括起来,则前面加一个反斜杠以形成\$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):
Or this:
Or this:
It's a mess, I know. But it should work.
$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.