"Shell" (外部評価システム)
ExternalEvaluateの使用法
- ExternalEvaluate["Shell",code] はオペレーティングシステムのシェルでコマンド列を実行し,Wolfram言語式として結果を返す.
- ExternalEvaluate["Shell"form,code] はコマンド列を実行し,指定された form で結果を返す.form には"Expression","StandardOutput","StandardError","ExitCode","Association"が指定できる.
例題
すべて開く すべて閉じる例 (2)
ExternalEvaluate["Shell", "hostname"]コマンド"ExitCode"が0でない場合,Failureオブジェクトが返される:
ExternalEvaluate["Shell", "not a command"]>とタイプして,ExternalEvaluateを使って評価を行うShellコードのセルを取得する:
Fileラッパーを使ってファイルに含まれるコードを実行する:
path = Export[CreateFile[], "echo 'hello from file!'", "String"]Unixシステムの場合,ファイルが実行可能であることを確認する:
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]"ReturnType"を使うと異なる戻り値の型を指定することができる:
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"]Unixシステムでは,ファイルが実行可能であることを確認する:
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|>
]2個以上の引数が必要な場合は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)
Spotlightを使って,Mac上の特定のファイルを検索する:
ローカルのGitリポジトリが最新であることを確認する関数を作成する:
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を使って,使用中のマシンで利用できるEvaluatorsを見付ける:
FindExternalEvaluators["Shell"]"Evaluator"を使って,マシンのシェルを手動で指定する:
ExternalEvaluate[{"Shell", "Evaluator" -> "/bin/zsh"}, "hostname"]Windowsシステムでは,Powershell.exeは有効な"Evaluator"である:
powershell = FileNameJoin@{Environment["SystemRoot"], "system32", "WindowsPowerShell", "v1.0", "powershell.exe"}ExternalEvaluate[{"Shell", "Evaluator" -> powershell}, "hostname"]RunProcessを使ってもシェルコマンドを実行することができるが,インタラクティブな出力はしない:
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"];$Outputを使うとPrint出力を非表示にすることができる:
Block[
{$Output = {}},
ExternalEvaluate[
"Shell" -> "StandardOutput",
"ping wikipedia.com -c 4"
]
]関連するガイド
履歴
2021 で導入 (12.3)