Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve external command output in Julia

Tags:

julia

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.

like image 260
Emile Avatar asked Jan 01 '23 05:01

Emile


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
like image 123
fredrikekre Avatar answered Feb 20 '23 19:02

fredrikekre