Erlang中执行linux命令的两种方法(如何执行erc命令)墙裂推荐

随心笔谈11个月前发布 admin
95 0

my_exec(Command) ->

    Port=open_port({spawn, Command}, [stream, in, eof, hide, exit_status]),

    Result=get_data(Port, []),

    Result.

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.

© 版权声明

相关文章