首页 > 解决方案 > gawk5 有没有办法并行启动 shell 命令?

问题描述

使用 gawk 5,我们可以启动 shell 命令,例如:

command = "echo toto"
command | getline

但是,这会挂起,直到命令完成,例如:

BEGIN {
    command = "{ echo start >> test.log ; sleep 10 ; echo stop >> test.log ;}"
    command | getline
    print "terminated"
}

我尝试&在命令末尾使用 a ,但没有成功:

BEGIN {
    command = "{ echo start >> test.log ; sleep 10 ; echo stop >> test.log ;} &"
    command | getline
    print "terminated"
}

我希望能够将 shell 命令作为新进程启动并“忘记”它们。例如:

BEGIN {
    command1 = "dosomething.sh &"
    command1 | getline
    command2 = "dosomething2.sh &"
    command2 | getline
    print "terminated"
}

这可以使用 gawk 5 来实现吗?

标签: awk

解决方案


| getline等待阅读。所以输出一些东西以便它可以读取它。

$ awk 'BEGIN { cmd="{ echo something; ( echo start >&2 ; sleep 1; echo stop >&2 ) & }"; cmd | getline; print "Awk end"; }' <<<'' ; echo "Awk end"; sleep 2
Awk end
start
After awk
stop

推荐阅读