期待与Awk逃脱 [英] Expect Escaping with Awk

查看:82
本文介绍了期待与Awk逃脱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将其分配给我的期望脚本中的值之前,我需要通过awk处理单个记录psql查询的输出.

I need to process the output of a single record psql query through awk before assigning it to a value in my expect script.

相关代码:


spawn $env(SHELL)
send "psql -U safeuser -h db test -c \"SELECT foo((SELECT id FROM table where ((table.col1 = \'$user\' AND table.col2 IS NULL) OR table.col2 = \'$user\') AND is_active LIMIT 1));\" | /bin/awk {{NR=3}} {{ print $1 }}; \r"
expect "assword for user safeuser:"
send "$safeuserpw\r"
expect -re '*'
set userpass $expect_out(0, string)

运行脚本时,我得到:

生成/bin/bash无法读取"1":没有这样的变量发送" psql -U safeuser -h db test -c \"SELECT foo((SELECT id FROM table where((table.col1 = \'$ user \'AND table.col2 ..."

spawn /bin/bash can't read "1": no such variable "send "psql -U safeuser -h db test -c \"SELECT foo((SELECT id FROM table where ((table.col1 = \'$user\' AND table.col2..."

在这里我缺少什么明显的东西吗?我的印象是双花括号保护了awk代码块.

Is there something glaring that I'm missing here? I was under the impression that the double curly-brackets protected the awk code block.

推荐答案

awk脚本将显示所有行,因为您在条件表达式中使用的是'='而不是'=='.请尝试以下操作:

The awk script will show all lines because you're using '=' instead of '==' in the conditional expression. Try the following:

spawn $env(SHELL)
send "psql -U safeuser -h db test -c \"SELECT foo((SELECT id FROM table where ((table.col1 = \'$user\' AND table.col2 IS NULL) OR table.col2 = \'$user\') AND is_active LIMIT 1));\" | /bin/awk \'NR==3 { print $1 }\'; \r"
expect "assword for user safeuser:"
send "$safeuserpw\r"
expect -re '*'
set userpass $expect_out(0, string)

这篇关于期待与Awk逃脱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆