2017-08-02 57 views
0

我想这样做:如何在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 

我应该拆分得到的字符串到每个分出一部分(原因有没有外壳做这个工作?)

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

非常感谢

+1

尝试'arg =“-l”'而不是'arg =“-l”'。这些空间混淆了'ls'。另外,请查看手册中的[运行外部程序](https://docs.julialang.org/en/latest/manual/running-external-programs/)。 –

+0

我认为这是同样的问题。你的意思是这样的:朱莉娅> ARG = “ - 1” “-l” 朱莉娅> CMD = “LS $ ARG” “ls -l命令” 朱莉娅 - >运行('$ cmd') 错误:无法不生成''ls -l'':没有这样的文件或目录(ENOENT), –

+0

使用'cmd = \'ls $ arg \''(带反引号) –

回答

1

尝试:

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

而且读running external programs 了解更多。

+0

好!对我的错误感到抱歉... 它也适用于更多变量:) 'arga =“-l”; argb =“-a”; cmd = \'ls $ arga $ argb \';运行(cmd)' –

+0

我搜索了关于运行外部程序的Julia文档,但没有发现关于退出状态......任何想法? –

+1

在链接的文档中,有一个段落,* run方法本身不返回任何内容,并且如果外部命令无法成功运行*,则会引发ErrorException。应该适用(虽然我现在还没有尝试过) –