Piping into an external program from Julia output

The problem is run, it should not be used here: run will send the output to stdout, or to /dev/null if wait=true (unless you play with undocumented arguments). You should give the result of pipeline directly to myReverse.

A pipeline is a Cmd. As the documentation says,

You can use [the Cmd] object to connect the command to others via pipes, run it, and read or write to it.

So the read in myReverse can work directly on the Cmd object.

The pipeline execution will start when the Cmd is opened, and the output can then be read from the Cmd as if it was a file. This is what read does: Doing @less read(`ls`, String) shows that it redirects to the following method:

function read(cmd::AbstractCmd)
    procs = open(cmd, "r", devnull)
    bytes = read(procs.out)
    success(procs) || pipeline_error(procs)
    return bytes
end

Contrary to run, the open call returns a process from which one can read the pipeline output, see @less open(`ls`). That’s why I use open rather than run in some of the examples above. But in simple cases you should just read directly from the Cmd.

3 Likes