-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.erl
More file actions
31 lines (28 loc) · 816 Bytes
/
exec.erl
File metadata and controls
31 lines (28 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
-module(exec).
-export([shell_exec/1]).
shell_exec(Command) ->
Port = open_port({spawn, Command}, [stream, in, stderr_to_stdout, 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
% force context switch
after 1 -> ok
end,
ExitCode =
receive
{Port, {exit_status, Code}} ->
Code
end,
{ExitCode, lists:flatten(Sofar)}
end.