[erlang-questions] how: binary from pipe in os:cmd

Geoff Cant <>
Mon Jan 14 22:03:16 CET 2008


Miguel Rodríguez Rubinos <> writes:

> Hello all,
>
> I'd like to read a binary from 'os:cmd("some_command with_args -")'.
> This generates a binary that is sent through stdout as result.
>
> The problem is that 'os:cmd' seems to stop reading when bytes 0x00
> (NULL) or 0x04 (EOT) are found. So, usually, it doesn't return the
> entire binary.
>
> Has anybody idea of how to do this? I would like to read the entire
> binary, not only the first N characters until 0x00 or 0x04.
>
> For example, if you type
>
> 33> length(os:cmd("lame --silent bark.wav -")).    
> 135
>
> Tha character at position 135 of resulting mp3 is 0x04. Besides,
> full mp3 size should be 576 bytes.
>
> Thanks in advance.
>
> Cheers,
>
> 	Miguel

Hi there - I've just looked at os.erl and it does filter the output of
the process though a function that stops on EOT (see os:eot/1) - can't
quite figure out why it stops on 0x00 as well.

As an alternative, maybe try reading it manually from a port spawn:

file:write_file("priv/test.bin", <<"This is a test of file reading.",
                                   4,"Just send end of transmission.",
                                   0,"Just send a null. Now EOF.">>).
 --> ok
os:cmd("cat priv/test.bin").
 --> <<"This is a test of file reading.">>

Port = open_port({spawn, "cat priv/test.bin"}, [stream, eof,
                 exit_status, binary]).
 --> <#Port<something>>
Data = receive {Port, {data, Bin}} -> Bin after 100 -> timeout end.
 --> <<"This is a test of file reading."...>>
End = receive {Port, {exit_status, Exit}} -> Exit after 100 -> timeout end.
 --> 0

Data =:= <<"This is a test of file reading.",4,"Just send end of
transmission.",0,"Just send a null. Now EOF.">>.
 --> True

Cheers,
-- 
Geoff Cant




More information about the erlang-questions mailing list