Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
希望带着Awk逃跑_Awk_Expect - Fatal编程技术网

希望带着Awk逃跑

希望带着Awk逃跑,awk,expect,Awk,Expect,我需要通过awk处理单记录psql查询的输出,然后将其分配给expect脚本中的值 有关守则: 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));\" | /

我需要通过awk处理单记录psql查询的输出,然后将其分配给expect脚本中的值

有关守则:

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 fooSELECT id从table.col1=\'$user\'和table.col2中的表中


这里有什么我遗漏的东西吗?我的印象是双花括号保护了awk代码块。

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

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)

tcl正在评估您的发送行,因为它在引号中。如果您想按原样传递它,您应该更改awk部分以转义$:


…|/bin/awk'NR==3{print\$1}'\r

谢谢。上次尝试通过SSH隧道将同一命令路由到另一个盒子时,由于太过困难,无法找到返回的方法。谢谢。可能是真的,但现在没有办法测试它。那是几年前的事了=