Retrieve external command output in Julia

Viewed 338

The system command 'stty size' returns two integers which give the dimension of the current terminal. How can Julia execute this command and return the outputs in two integer variables.

1 Answers

You can create a pipeline and redirect stdout to a buffer, and then parse the string:

julia> io = IOBuffer();

julia> cmd = pipeline(`stty size`; stdout=io, stderr=devnull);

julia> run(cmd);

julia> str = String(take!(io))
"60 211\n"

julia> a, b = parse.(Int, split(strip(str)));

julia> a
60

julia> b
211

Note: Normally one can just read the command directly, e.g. read(`stty size`), String), but for this particular command it does not seem to work, I think this is because a proper tty is not set up in that case):

julia> read(`stty size`, String)
stty: 'standard input': Inappropriate ioctl for device
Related