在POSIX awk中,如何通过命令|处理其输出后从命令获取退出状态(返回代码) getline var?如果命令以非零退出状态退出,我希望我的awk脚本退出1.
例如,假设我有一个名为foo.awk的awk脚本,如下所示:
function close_and_get_exit_status(cmd) { # magic goes here... } BEGIN { cmd = "echo foo; echo bar; echo baz; false" while ((cmd | getline line) > 0) print "got a line of text: " line if (close_and_get_exit_status(cmd) != 0) { print "ERROR: command '" cmd "' Failed" | "cat >&2" exit 1 } print "command '" cmd "' was successful" }
然后我希望发生以下情况:
$awk -f foo.awk got a line of text: foo got a line of text: bar got a line of text: baz ERROR: command 'echo foo; echo bar; echo baz; false' Failed $echo $? 1
根据POSIX specification for awk,命令| getline为成功输入返回1,为文件结束返回0,为错误返回-1.如果命令以非零退出状态退出,则不是错误,因此不能用于查看命令是否已完成且失败.
类似地,close()不能用于此目的:close()仅在关闭失败时返回非零,而不是在关联命令返回非零退出状态时返回非零. (在gawk中,close(命令)返回命令的退出状态.这是我想要的行为,但我认为它违反了POSIX规范,并不是所有的awk实现都是这样的.)
解决方法
最简单的方法是在命令执行后从shell回显退出状态,然后使用getline读取它.例如
$cat tst.awk BEGIN { cmd = "echo foo; echo bar; echo baz; false" mod = cmd "; echo \"$?\"" while ((mod | getline line) > 0) { if (numLines++) print "got a line of text: " prev prev = line } status = line close(mod) if (status != 0) { print "ERROR: command '" cmd "' Failed" | "cat >&2" exit 1 } print "command '" cmd "' was successful" } $awk -f tst.awk got a line of text: foo got a line of text: bar got a line of text: baz ERROR: command 'echo foo; echo bar; echo baz; false' Failed $echo $? 1
如果有人正在阅读此内容并考虑使用getline,请务必阅读http://awk.freeshell.org/AllAboutGetline并完全理解所有注意事项以及首先要做的事情.