运行 IPython 时,我可以运行任何前面带有感叹号的 shell 命令:
In [1]: !ls
file1 file2 file3 file4
In [2]: !ls file*
file1 file2 file3 file4
Run Code Online (Sandbox Code Playgroud)
但是,某些事情(特别是使用 if 通配符)在 shell 模式下(在您输入分号之后)在 Julia REPL 上不起作用:
shell> ls file
file1 file4 file2 file3
shell> ls file*
ls: cannot access 'file*': No such file or directory
Run Code Online (Sandbox Code Playgroud)
我在这里错过了什么吗?有没有办法让通配符像在 Linux shell 中一样正常运行?
你是对的,Julia shell REPL 模式不是大多数人期望的 shell,可以在这里和这里找到一些讨论。目前,参数按原样传递,没有外壳扩展,因此如果程序支持正常工作的文字“*”:
shell> find -iname *
.
./file1
./file2
./file3
./file4
Run Code Online (Sandbox Code Playgroud)
如果不是,你需要想出另一种方式。就像通过 bash 调用一样:
shell> bash -c 'ls *'
file1 file2 file3 file4
Run Code Online (Sandbox Code Playgroud)
或者使用 Julia 函数/宏:
julia> bash(str::String) = run(`bash -c $str`)
bash (generic function with 1 method)
julia> macro sh_str(str::String)
bash(str)
end
julia> sh"ls *"
file1 file2 file3 file4
Process(`bash -c 'ls *'`, ProcessExited(0))
Run Code Online (Sandbox Code Playgroud)