我对 Windows 一点也不熟悉,但我敢肯定,你应该看看 这个。这是实现os:cmd/1
功能。
有问题os:cmd/1
。此功能不会让您知道命令执行是否成功,因此您只需要依赖某些命令外壳行为(取决于平台)。
我建议你使用erlang:open_port/2
函数。像这样的东西:
my_exec(Command) ->
Port = open_port({spawn, Command}, [stream, in, eof, hide, exit_status]),
get_data(Port, []).
get_data(Port, Sofar) ->
receive
{Port, {data, Bytes}} ->
get_data(Port, [Sofar|Bytes]);
{Port, eof} ->
Port ! {self(), close},
receive
{Port, closed} ->
true
end,
receive
{'EXIT', Port, _} ->
ok
after 1 -> % force context switch
ok
end,
ExitCode =
receive
{Port, {exit_status, Code}} ->
Code
end,
{ExitCode, lists:flatten(Sofar)}
end.
因此函数my_exec/1
将返回进程退出代码以及进程标准输出。