1

i'd like to do something like :

arg= " -l "
cmd = "ls $arg "
run(cmd)

but i can find no simple solution to do that :

$ julia
               _
   _       _ _(_)_     |  A fresh approach to technical computing
  (_)     | (_) (_)    |  Documentation: https://docs.julialang.org
   _ _   _| |_  __ _   |  Type "?help" for help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 0.6.0 (2017-06-19 13:05 UTC)
 _/ |\__'_|_|_|\__'_|  |  Official http://julialang.org/ release
|__/                   |  x86_64-pc-linux-gnu

julia> arg=" -l "
" -l "

julia> cmd=`ls $arg`
`ls ' -l '`

julia> run(cmd)
ls: cannot access  -l : No such file or directory
ERROR: failed process: Process(`ls ' -l '`, ProcessExited(2)) [2]
Stacktrace:
 [1] pipeline_error(::Base.Process) at ./process.jl:682
 [2] run(::Cmd) at ./process.jl:651

julia> cmd="ls $arg"
"ls  -l "

julia> run(`$cmd`)
ERROR: could not spawn `'ls  -l '`: no such file or directory (ENOENT)
Stacktrace:
 [1] _jl_spawn(::String, ::Array{String,1}, ::Ptr{Void}, ::Base.Process, ::RawFD, ::RawFD, ::RawFD) at ./process.jl:360
 [2] #373 at ./process.jl:512 [inlined]
 [3] setup_stdio(::Base.##373#374{Cmd}, ::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:499
 [4] #spawn#372(::Nullable{Base.ProcessChain}, ::Function, ::Cmd, ::Tuple{RawFD,RawFD,RawFD}) at ./process.jl:511
 [5] run(::Cmd) at ./process.jl:650

should i split the resulting string to separate each part (cause there is no shell doing the job ?)

by the way, how to get the exit status of the command ?

thanks a lot

6
  • 1
    Try arg = "-l" instead of arg = " -l ". The spaces confused ls. Also, look at Running External Programs from the manual. Commented Aug 2, 2017 at 10:49
  • i think it's the same problem. you mean like that : julia> arg="-l" "-l" julia> cmd="ls $arg" "ls -l" julia> run($cmd) ERROR: could not spawn 'ls -l': no such file or directory (ENOENT), Commented Aug 2, 2017 at 12:18
  • Use cmd=`ls $arg` (with backticks) Commented Aug 2, 2017 at 12:20
  • no better... (backticks are remove in my comment :( ) julia> arg=" -l " " -l " julia> cmd=ls $arg ls ' -l ' julia> run(cmd) ls: impossible d'accéder à -l : Aucun fichier ou dossier de ce type ERROR: failed process: Process(ls ' -l ', ProcessExited(2)) [2] Stacktrace: [1] pipeline_error(::Base.Process) at ./process.jl:682 [2] run(::Cmd) at ./process.jl:651 Commented Aug 2, 2017 at 12:21
  • 1
    Just cut and paste the following: arg = "-l" ; cmd = `ls $arg` ; run(cmd) Commented Aug 2, 2017 at 12:24

1 Answer 1

1

Try:

arg = "-l"
cmd = `ls $arg`
run(cmd)

And read running external programs to understand more.

Sign up to request clarification or add additional context in comments.

5 Comments

ok ! sorry for my mistakes... and it works with more variables too :) arga = "-l" ; argb = "-a" ; cmd = `ls $arga $argb` ; run(cmd)
i searched the Julia doc about running external programs but did not found about the exit status... any idea ?
In the doc linked, there is a paragraph with The run method itself returns nothing, and throws an ErrorException if the external command fails to run successfully. Should apply (though I haven't tried it now)
ok. so it's still a bit hard to use for real cases. it's confusing too not to be able to use something like : arg = "-l -a"
Perhaps it is only confusing at the beginning. You might want to try: args = ["-l","-a"] ; cmd = `ls $args` ; run(cmd). This also enables you to have spaces in arguments and filenames, something which a normal shell command would make difficult. Another solution is: cmd = "ls -l -r" ; run(`bash -c $cmd`) which uses the shell to run command. But the last solution depends on the specific shell and OS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.