是否有一个Erlang操作系统:cmd等效,它采用字符串列表而不是单个命令?

是否有Erlang调用我可以做Retval = subprocess:call([“cmd”,“arg1”,“arg2”,“arg3”])?

如果我正在构建一个复杂的命令来执行,使用os:cmd / 1可以很容易地逃避错误.比较Python的subprocess.call()方法,我传入一个字符串列表并知道它是逐字传递给子进程的,没有任何损坏.

谢谢.

这就是我想出的.

-module(mycmd).
-export([cmd/2]).

cmd(Cmd, Args) ->
    Tag = make_ref(), 
    {Pid, Ref} = erlang:spawn_monitor(fun() ->
            Rv = cmd_sync(Cmd, Args),
            exit({Tag, Rv})
        end),
    receive
        {'DOWN', Ref, process, Pid, {Tag, Data}} -> Data;
        {'DOWN', Ref, process, Pid, Reason} -> exit(Reason)
    end.

cmd_sync(Cmd, Args) ->
    P = open_port({spawn_executable, os:find_executable(Cmd)}, [
            binary, use_stdio, stream, eof, {args, Args}]),
    cmd_receive(P, []).

cmd_receive(Port, Acc) ->
    receive
        {Port, {data, Data}} -> cmd_receive(Port, [Data|Acc]);
        {Port, eof}          -> {ok, lists:reverse(Acc)}
    end.
相关文章
相关标签/搜索