当我在下面的示例中调用不带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
从文档中我不了解行为上的差异。
答案 0 :(得分:5)
当您在cat >bar
命令中重定向文件时,命令shell关闭stdout
并且在这种情况下Erlang只关闭端口,因为端口尝试默认使用命令输出并在eof
关闭。解决问题的正确方法是使用out
选项作为已建议的 butter71 。 out
,exit_status
,error_to_stdout
等选项告诉端口不要担心stdout
。
答案 1 :(得分:4)
尝试仅为输出设置端口:
P = open_port({spawn, "cat >bar"}, [stream, use_stdio, out]).
port_command(P, "hello\n").
我的猜测是,因为你重定向了命令的标准输出,所以erlang不满意。