I wanted to query if a program is installed in a Linux system. From bash I would do something as type -p program or similar, as explained here.
If I try something such as read(`type -p program`) within julia I get an error. command -v program or hash program also do not work. If I try which program I get an error if the program does not exist. Any ideas?
In the meantime I am querying if the file/program exists in a known location. Thanks!
I don’t think your suggestion works, I get an error. Secondly, read or run are very similar, only that read catches stdout and run returns nothing instead.
The problem I think is that run or read do not call the shell at all, and although many standard bash calls have been implemented (such as tail, less, cat, ls or others), hash, type or command haven’t. I do not know if they should, though.
julia> a = run(`type -p ls`)
ERROR: IOError: could not spawn `type -p ls`: no such file or directory (ENOENT)
Stacktrace:
[1] _jl_spawn(::String, ::Array{String,1}, ::Cmd, ::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:367
[2] (::getfield(Base, Symbol("##493#494")){Cmd})(::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:509
[3] setup_stdio(::getfield(Base, Symbol("##493#494")){Cmd}, ::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:490
[4] #_spawn#492(::Nothing, ::Function, ::Cmd, ::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:508
[5] _spawn at ./process.jl:504 [inlined]
[6] #run#503(::Bool, ::Function, ::Cmd) at ./process.jl:662
[7] run(::Cmd) at ./process.jl:661
[8] top-level scope at none:0
Well, chomp(join(map(Char, read(bash -c “type -p ls”)))) does.
What a mouthful, I don’t know if it can be shorter. Only chomp didn’t work, because I get an array.
This is a bit old so maybe by now there is a better way (I couldn’t find it ). What worked for me is invoking the command and looking for IOErrors. If the command is found and invoked but the command itself is incorrectly invoked, different kinds of errors will result.
function run(command::Cmd)
try Base.run(command)
catch ex
if isa(ex, IOError)
@error "Can not find $(command.exec[1]). Please make sure that $(command.exec[1]) is installed and accessible."
end
rethrow(ex)
end
end