将系统命令的输出分配给变量
我想在 awk 脚本中运行 system
命令并将其输出存储在变量中。我一直在尝试这样做,但命令的输出总是发送到 shell,而我无法捕获它。关于如何做到这一点有什么想法吗?
示例:
$ date | awk --field-separator=! {$1 = system("strip $1"); /*more processing*/}
应调用 strip
系统命令,而不是将输出发送到 shell,而应将输出分配回 $1
以进行更多处理。现在,它将输出发送到 shell 并将命令的 retcode 分配给 $1
。
I want to run the system
command in an awk script and get its output stored in a variable. I've been trying to do this, but the command's output always goes to the shell and I'm not able to capture it. Any ideas on how this can be done?
Example:
$ date | awk --field-separator=! {$1 = system("strip $1"); /*more processing*/}
Should call the strip
system command and instead of sending the output to the shell, should assign the output back to $1
for more processing. Rignt now, it's sending output to shell and assigning the command's retcode to $1
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
注意:协进程是 GNU awk 特定的。
无论如何,另一种选择是使用 getline
调用
close(cmd)
将防止awk
在多次调用后抛出此错误:Note: Coprocess is GNU awk specific.
Anyway another alternative is using getline
Calling
close(cmd)
will preventawk
to throw this error after a number of calls :要在
awk
中运行系统命令,您可以使用system()
或cmd |获取行
。我更喜欢 cmd | getline 因为它允许您将值捕获到变量中:
更一般地,您可以将命令设置到变量中:
请注意,使用
close()
来防止获取“非常重要”如果您有多个结果,则会出现太多打开的文件”错误(感谢 mateuscb 在评论中指出这一点)。使用
system()
,命令输出会自动打印,您可以捕获的值是它的返回码:To run a system command in
awk
you can either usesystem()
orcmd | getline
.I prefer
cmd | getline
because it allows you to catch the value into a variable:More generally, you can set the command into a variable:
Note it is important to use
close()
to prevent getting a "makes too many open files" error if you have multiple results (thanks mateuscb for pointing this out in comments).Using
system()
, the command output is printed automatically and the value you can catch is its return code:想通了。
我们使用awk的双向I/O
将 $1 传递给 strip,getline 将 strip 的输出返回到 $1
Figured out.
We use awk's Two-way I/O
passes $1 to strip and the getline takes output from strip back to $1
当您需要处理 grep 输出时,可以使用此选项:
选项
-F:
告诉 awk 使用:
作为字段分隔符"basename "$1"" 在第一个字段
|& 上执行 shell 命令
读取子流中前一个 shell 命令的输出basename
getline $1You can use this when you need to process a grep output:
option
-F:
tell awk to use:
as field separator"basename "$1""
execute shell commandbasename
on first field|& getline $1
reads output of previous shell command in substream我正在使用 macOS 的
awk
,并且我还需要命令的退出状态。所以我扩展了 @ghostdog74 的解决方案来获取退出状态:如果非零退出状态则退出:
所以基本上我只是更改了
cmd
以在新行上打印命令的退出状态。执行上述while
循环后,res
将包含命令的退出状态和value
将包含命令的完整输出。老实说这不是一个很好的方法,我本人想知道是否有更好的方法。
I am using macOS's
awk
and I also needed exit status of the command. So I extended @ghostdog74's solution to get the exit status too:Exit if non-zero exit status:
So basically I just changed
cmd
to print exit status of the command on a new line. After the execution of the abovewhile
loop,res
would contain the exit status of the command andvalue
would contain the complete output of the command.Honestly not a very neat way and I myself would like to know if there is some better way.