zwlayer
zwlayer

Reputation: 1824

How to run external program that takes its input with echo from julia

I have a very basic problem:

I have a file and I need to run a command for each word inside. Lets assume the word I have now is "example_word" the command I need to run is as follows:

readall(run(pipeline(`echo example_word`,`flookup model.foma`))

Unfortunatelly I cannot get the output of this command from repl. I search on the net and readstring is recommended for it. When I try it I got the following error:

julia> read(pipeline(`echo example_word`,`flookup model.foma`),String)
ERROR: MethodError: no method matching read(::Base.OrCmds, ::Type{String})
Closest candidates are:
  read(::AbstractString, ::Any...) at io.jl:109
  read{T}(::IO, ::Type{T}, ::Int64, ::Int64...) at io.jl:235
  read{T}(::IO, ::Type{T}, ::Integer, ::Integer...) at io.jl:236
  ...

julia> readstring(pipeline(`echo example_word`,`flookup model.foma`),String)
ERROR: MethodError: no method matching readstring(::Base.OrCmds, ::Type{String})
Closest candidates are:
  readstring(::Base.AbstractCmd) at process.jl:581
  readstring(::Base.AbstractCmd, ::Union{Base.FileRedirect,IO,RawFD}) at process.jl:581

Upvotes: 0

Views: 142

Answers (1)

Alexander Morley
Alexander Morley

Reputation: 4181

String(read(pipeline(`echo example_word`,`cat`)))

or (as @DanGetz suggested)

readstring(pipeline(`echo example_word`,`cat`))

Just replace cat with the command you want to use, I didn't have it on my computer.

I the future you might have realised that you didn't need to give readstring the String parameter by looking at the error message a bit closer :)

ERROR: MethodError: no method matching readstring(::Base.OrCmds, ::Type{String})
Closest candidates are:
  readstring(::Base.AbstractCmd) at process.jl:581

Upvotes: 2

Related Questions