"Shell" (外部计算系统)
ExternalEvaluate 用法
- ExternalEvaluate["Shell",code] 在操作系统 shell 中执行代码字符串并以 Wolfram 语言表达式的形式返回结果.
- ExternalEvaluate["Shell"form,code] 执行命令字符串并以指定 form 的形式返回结果. form 可能的规格有 "Expression"、"StandardOutput"、"StandardError"、"ExitCode" 和 "Association".
范例
打开所有单元 关闭所有单元基本范例 (2)
ExternalEvaluate["Shell", "hostname"]若命令 "ExitCode" 非零,则会返回一个 Failure 对象:
ExternalEvaluate["Shell", "not a command"]类型 > 可得到一个使用 ExternalEvaluate 进行计算的 Shell 代码单元格:
用 File 封装运行文件中包含的代码:
path = Export[CreateFile[], "echo 'hello from file!'", "String"]ExternalEvaluate["Shell", "chmod +x <* path *>"]ExternalEvaluate["Shell", File[path]]用 CloudDeploy 部署代码,从 CloudObject 直接运行代码:
cobj = CloudExport["echo 'hello from cloud'", "String"]ExternalEvaluate["Shell", cobj]用 URL 封装直接运行在线上托管的代码:
ExternalEvaluate["Shell", URL["https://exampledata.wolfram.com/script.sh"]]范围 (20)
session = StartExternalSession["Shell"]res = ExternalEvaluate[session, "hostname"]res["ExitCode"]res["Command"]从结果中提取 "StandardOutput" 和 "StandardError":
res[{"StandardOutput", "StandardError"}]DeleteObject[session]ExternalEvaluate[
{"Shell", "ReturnType" -> "ExitCode"},
"not a command"
]ExternalEvaluate[
{"Shell", "ReturnType" -> "StandardOutput"},
"hostname"
]ExternalEvaluate[
{"Shell", "ReturnType" -> "Association"},
"hostname"
]会话的选项 (10)
"ReturnType" (3)
对于 Shell 系统,默认返回的类型是 "Expression":
ExternalEvaluate["Shell", "hostname"]对于 "Expression" 返回类型,结果会是 Success 或 Failure,取决于退出代码:
result = ExternalEvaluate["Shell", "rm not/a/folder"]可用 "StandardOutput"、"StandardError" 和 "ExitCode" 属性提取其他有关程序输出的信息:
result["ExitCode"]result[{"StandardError", "StandardOutput"}]"ReturnType" 可取的值有 "Expression"、"StandardOutput"、"StandardError" 和 "ExitCode":
ExternalEvaluate[{"Shell", "ReturnType" -> "ExitCode"}, "hostname"]ExternalEvaluate[{"Shell", "ReturnType" -> "StandardOutput"}, "date"]ExternalEvaluate[{"Shell", "ReturnType" -> "StandardError"}, "rm not/a/file"]ExternalEvaluate[{"Shell", "ReturnType" -> "Expression"}, "date"]可用 Rule 快速指定 "ReturnType":
ExternalEvaluate["Shell" -> "ExitCode", "hostname"]“Evaluator" (2)
ExternalEvaluate[{"Shell", "Evaluator" -> "/bin/sh"}, "ps -p $$"]在 Windows 上,通过以下方式用 cmd.exe 运行指令:
ExternalEvaluate[{"Shell", "Evaluator" -> Environment["ComSpec"]}, "Whoami"]如果想要在 Windows PowerShell 上运行指令,可使用以下方式:
ExternalEvaluate[{"Shell", "Evaluator" -> FileNameJoin@{Environment["SystemRoot"], "system32", "WindowsPowerShell", "v1.0", "powershell.exe"}}, "hostname"]"SessionProlog" (2)
"SessionEpilog" (1)
"Prolog" (1)
指令的选项 (8)
"Command" (5)
ExternalEvaluate["Shell", "date"]ExternalEvaluate["Shell", <|"Command" -> "date"|>]path = Export[CreateFile[], "echo 'hello from file!'", "String"]ExternalEvaluate["Shell", "chmod +x <* path *>"]用 File 封装运行文件中的代码:
ExternalEvaluate["Shell", <|"Command" -> File[path]|>]ExternalEvaluate["Shell", File[path]]ExternalEvaluate["Shell", helloWorld]用 URL 封装直接运行线上的代码:
ExternalEvaluate["Shell", <|"Command" -> URL["https://exampledata.wolfram.com/script.sh"]|>]ExternalEvaluate["Shell", URL["https://exampledata.wolfram.com/script.sh"]]将代码放入 CloudObject 中:
cloudObj = CloudExport["echo 'hello from cloud'", "Text", CloudObject["hello-world-sh"]]ExternalEvaluate["Shell", <|"Command" -> cloudObj|>]ExternalEvaluate["Shell", cloudObj]"ReturnType" (1)
"TemplateArguments" (2)
运行命令时可内嵌一个 TemplateExpression:
x = RandomReal[]
ExternalEvaluate["Shell", "echo <* x *>"]可用 "TemplateArguments" 填充 TemplateSlot:
ExternalEvaluate[
"Shell",
<|"Command" -> "echo ``", "TemplateArguments" -> 1|>
]如果需要多个参数,可以使用 List:
ExternalEvaluate[
"Shell",
{
<|"Command" -> "echo `` ``", "TemplateArguments" -> {1, 2}|>,
<|"Command" -> "echo `1` `2`", "TemplateArguments" -> {1, 2}|>,
<|"Command" -> "echo `2` `1`", "TemplateArguments" -> {1, 2}|>
}
]可以命名模板插槽,并用 Association 将已命名参数传递给模板:
ExternalEvaluate[
"Shell",
<|"Command" -> "echo `year`/`month`/`day`", "TemplateArguments" -> <|"year" -> 2021, "month" -> 10, "day" -> 2|>|>
]应用 (3)
cloneRepo[url_, dest_, branch_ : "master"] :=
ExternalEvaluate[
"Shell", Flatten @ {
If[
! FileExistsQ[FileNameJoin[{dest, ".git"}]],
"git clone " <> url <> " " <> dest,
{}
],
"cd " <> dest,
"git checkout " <> branch,
"git pull --all"
}
]cloneRepo[
"https://github.com/hpaluch/rust-hello-world",
"~/Downloads/myClonePath",
"master"
]用 Shell 自动下载 Git 存储库,从源代码构建程序并运行该程序:
ExternalEvaluate[
"Shell", {
"echo 🧹 cleanup the environment",
"rm -rf ~/Downloads/rust-hello-world",
"echo 🧑🔬 download latest code",
"cd ~/Downloads",
"git clone https://github.com/hpaluch/rust-hello-world.git",
"cd ~/Downloads/rust-hello-world",
"echo 👷♀️ build the program",
"cargo update",
"cargo build",
"echo 🚀 run the program",
"cargo run"
}]属性和关系 (2)
用 FindExternalEvaluators 找出机器中所有可用的运算器:
FindExternalEvaluators["Shell"]用 "Evaluator" 在你的机器上手动指定一个 shell:
ExternalEvaluate[{"Shell", "Evaluator" -> "/bin/zsh"}, "hostname"]Powershell.exe 在 Windows 系统中是一个有效 "Evaluator":
powershell = FileNameJoin@{Environment["SystemRoot"], "system32", "WindowsPowerShell", "v1.0", "powershell.exe"}ExternalEvaluate[{"Shell", "Evaluator" -> powershell}, "hostname"]RunProcess 也可用于执行 shell 命令,但并不会交互式打印输出:
RunProcess[{"ping", "wikipedia.com", "-c", "4"}]ExternalEvaluate["Shell", "ping wikipedia.com -c 4"]可能存在的问题 (2)
若程序要求输入,则 ExternalEvaluate 无法中断:
TimeConstrained[
ExternalEvaluate[
"Shell",
"echo 'Hello, who am I talking to?' && read varname"
],
2
]当运行要求用户输入的外部命令时,建议将其在非交互模式下运行:
ExternalEvaluate["Shell", "python3 -m pip uninstall numpy -y"]ExternalEvaluate 实时打印输出:
ExternalEvaluate["Shell", "ping wikipedia.com -c 4"];Block[
{$Output = {}},
ExternalEvaluate[
"Shell" -> "StandardOutput",
"ping wikipedia.com -c 4"
]
]相关指南
-
▪
- 外部已诠释的语言接口 ▪
- 外部语言接口
历史
2021年引入 (12.3)