变量传递给 awk 使用的 shell 脚本

Variable passed to shell script used by awk

我的 bash 脚本中有以下内容(由另一个将变量传递给它的脚本调用):

psql -qtAX -h "" -p "" -U "" -d "" -f "/script/test.sql"

我现在想将其更改为:

psql -qtAX -h "" -p "" -U "" -d "" -f "/script/test.sql" |
awk {print ""}'

但是,这并没有给我正确的输出,因为 awk 使用的变量 $2 默认为传递给脚本(而不是从管道输出中获取它)。

psql -qtAX -h testhost -p 5432 -U user -f "/script/test.sql" |
awk {print ""}'

如何让它从管道输出中获取 $2 值?

你很接近。 awk 脚本通常是单引号以避免 awk 字段变量的 shell 插值(变量替换)。在您的情况下,</code> 对 shell 有意义,因此必须用单引号将其转义。</p> <p>试试这个:</p> <pre><code>psql -qtAX -h "" -p "" -U "" -d "" -f "/script/test.sql" | awk '{print }'