[erlang-questions] Executing external commands

Per Hedeland per@REDACTED
Mon Feb 12 20:30:09 CET 2007


Fernando Ipar <fipar@REDACTED> wrote:
>
>check(Command) ->
>        Output = os:cmd(Command ++ ";echo $?"),
>        {match, Start, Length} = 
>regexp:first_match(Output,".*\n[0-9][0-9]*\n"),
>        Pos = (Start + Length) - 2,
>        { string:substr(Output,Pos,1), Output}.

>4> ext:check("lsss /bin").
>{"7","sh: line 1: lsss: command not found\n127\n"}
  ^^^                                       ^^^

Oops!:-) If you want to stick with regexp, a more proper incantation
might be:

    {match, Start, Length} = regexp:match(Output,"\n[0-9]+\n$"),
    {string:substr(Output,Start+1,Length-2), Output}.

However I think this might be preferrable:

    Code = hd(lists:reverse(string:tokens(Output, "\n")))

--Per Hedeland



More information about the erlang-questions mailing list