你好!可以,Julia 能执行 shell[1] (#footnote-731542-1) 命令,Pluto 笔记本也一样。比如,把下面的代码放进单元格,就会在类 Unix 系统上执行列出目录的命令 ls,
run(`ls`)
它先构造一个表示命令的 Cmd 类型对象,再通过 run 执行。run 和 Cmd 的文档还有很多有用的细节,介绍如何用这种方式执行命令。
要注意,打开 Pluto 笔记本会执行所有单元格,而修改一个单元格时,依赖它的单元格也会重新执行。对于外部 shell 命令,这种行为是否合适取决于具体情况,不过也可以避免每次依赖变化时都执行这个“命令单元格”。
附言,run 返回进程的结果,因此通常会显示输出,如果命令失败则会显示错误码。
实际上,这些命令并不是通过 shell 执行的,下面的回复里有解释。↩︎ (#footnote-ref-731542-1)
从英语翻译·查看原文Hi! Yes, Julia can execute shell[1] (#footnote-731542-1) commands and so can a Pluto notebook, for example a cell with the following code executes Unix-like systems' directory-listing command ls:
run(`ls`)
It constructs an object with type Cmd representing the command, then executes it using run. The documentation for run and Cmd contains plenty of useful additional detail explaining how to execute commands in this way.
Be aware that opening a Pluto notebook executes every cell, and changing a cell also re-executes the cells depending on it. Whether that is desirable for external shell commands depends on your situation, though you can avoid executing that “command cell” whenever one of its dependencies changes.
PS: run returns the process result, meaning you generally see its output or error codes when the command fails.
Actually these commands don't execute through a shell, as explained in my later reply. ↩︎ (#footnote-ref-731542-1)