[erlang-questions] Executing external commands

Fernando Ipar <>
Mon Feb 12 14:10:38 CET 2007

Thanks a lot to everyone who replied.
I messed up some of my replies by not sending them to the list, I'm 
sorry for the out of list e-mails some of you

The port suggestion by Raimo and Richard are very good.
Richard's code runs perfectly on my linux system, and I can get the 
ExitCode with no problems.
One small question though:
After looking at the list D, I was under the impression it would be a 
list of ASCII codes, but list_to_atom(D) exits
with badarg. How do you get the output from that list?.

Michael's suggestion was great too, and if you're running just on unix 
it's probably easier.
Here's a function, based on his examples, that returns {ExitCode, Output}

check(Command) ->
        Output = os:cmd(Command ++ ";echo $?"),
        {match, Start, Length} = 
        Pos = (Start + Length) - 2,
        { string:substr(Output,Pos,1), Output}.

Here are two sample runs:

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

Thanks again for all the help.

More information about the erlang-questions mailing list