ShpielMeister
ShpielMeister

Reputation: 1455

can Julia capture the results shell command

I'd like to capture the result of a shell command in julia

in ipython this works:

[1]: x = ! date
In [2]: x
Out[2]: ['Thu Dec 14 15:34:06 PST 2017']

I've already tried

julia> x = ;date
ERROR: syntax: unexpected ;

julia> x = readstring(`date`)
"Thu Dec 14 21:33:48 PST 2017\n"
julia> x
"Thu Dec 14 21:33:48 PST 2017\n"

is readstring the best way to accomplish this? or is there a ; way, too?

Upvotes: 0

Views: 98

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69829

Pressing ; works only in Julia REPL moving it to >shell mode and only if you are at the beginning of the line.

In programs ; is expression terminator. Therefore x = ;date is an error because x = is a malformed expression.

Starting from Julia 0.7 you will not be able to use readstring but rather

read(`date`, String)

If you do not want a string but an array of raw bytes simply write

read(`date`)

Upvotes: 5

Related Questions