如何使用不同变量的连接在julia中组合外部命令?

时间:2017-08-02 10:33:25

标签: julia

我喜欢做类似的事情:

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

但我找不到这样做的简单解决方案:

$ 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

我应该将结果字符串分开以分隔每个部分(因为没有shell执行该作业吗?)

顺便说一句,如何获取命令的退出状态?

非常感谢

1 个答案:

答案 0 :(得分:1)

尝试:

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

阅读running external programs 了解更多。