当我在下面的示例中调用没有 exit_status
的 open_port 时,它无法使用:
Eshell V5.7.5 (abort with ^G)
1> P = open_port({spawn, "cat >bar"}, [stream, use_stdio]).
#Port<0.498>
2> port_command(P, "hello\n").
** exception error: bad argument
in function port_command/2
called as port_command(#Port<0.498>,"hello\n")
但是当我添加 exit_status
并保持一切不变时,它可以工作:
Eshell V5.7.5 (abort with ^G)
1> P = open_port({spawn, "cat >bar"}, [stream, use_stdio, exit_status]).
#Port<0.498>
2> port_command(P, "hello\n").
true
从文档中我不明白行为的差异。
最佳答案
当您在 cat >bar
中重定向文件中的输出时,命令 shell 会关闭 stdout
并且 Erlang 在这种情况下只会关闭端口,因为端口默认情况下会尝试使用命令输出并关闭 eof
。修复它的正确方法是使用 out
选项,正如 butter71 已经建议的那样。 out
、exit_status
、error_to_stdout
等选项告诉端口不要打扰 stdout
。
关于erlang - 未使用 `exit_status` 选项时,open_port 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6550206/