"Shell" (External Evaluation System)
ExternalEvaluate Usage
- ExternalEvaluate["Shell",code] executes a string in an operating system shell and returns the result as a Wolfram Language expression.
- ExternalEvaluate["Shell"form,code] executes the command string and returns the result in the specified form. Possible specifications for form are "Expression", "StandardOutput", "StandardError", "ExitCode" and "Association".
Examples
open allclose allBasic Examples (2)
Run a command in your default shell and return the result:
If a command "ExitCode" is not 0, a Failure object is returned:
Type > to get a Shell code cell that uses ExternalEvaluate to evaluate:
Use the File wrapper to execute code contained in a file:
On Unix systems, make sure the file is executable:
Deploy code using CloudDeploy and then run the code directly from a CloudObject:
Use a URL wrapper to directly run code hosted online:
Scope (20)
Evaluate a command and return the result:
Extract the "ExitCode" from the result:
Extract the original command from the result:
Extract the "StandardOutput" and "StandardError" from the result:
Use "ReturnType" to specify a different return type:
Session Options (10)
"ReturnType" (3)
For Shell systems, the default return type is "Expression":
With the return type of "Expression", the result will be a Success or a Failure depending on the exit code:
You can extract additional information about the program's output using the properties "StandardOutput", "StandardError" and "ExitCode":
Possible values for "ReturnType" are "Expression", "StandardOutput", "StandardError" and "ExitCode":
You can use a Rule to quickly specify the "ReturnType":
“Evaluator" (2)
"SessionProlog" (2)
Command Options (8)
"Command" (5)
When a string is provided, the command is directly executed:
The above is equivalent to writing the command using this form:
Create a file with a script in it:
Un Unix systems, make sure the file is executable:
Use a File wrapper to run the code from the file:
The above is equivalent to writing the command using this form:
Use a URL wrapper to directly run code hosted online:
The above is equivalent to writing the command using this form:
Put code in a CloudObject:
Evaluate directly from the cloud:
The above is equivalent to writing the command using this form:
"ReturnType" (1)
"TemplateArguments" (2)
When running a command, you can inline a TemplateExpression:
You can explicitly fill TemplateSlot using "TemplateArguments":
If you need more than one argument, you can use a List:
You can name template slots and use an Association to pass named arguments to the template:
Applications (3)
Get your local network address:
Use Spotlight to search for specific files on your Mac:
Show the 10 largest open files:
Create a function to make sure that a local Git repository is up to date:
Use Shell to automate the download of a Git repository, build the program from source and run the program:
Properties & Relations (2)
Use FindExternalEvaluators to find all available Evaluators in your machine:
Use "Evaluator" to manually specify a shell on your machine:
Powershell.exe is a valid "Evaluator" on a Windows system:
RunProcess can also be used to execute shell commands, but it does not print output interactively:
Possible Issues (2)
ExternalEvaluate is unable to interrupt if the program is asking for input:
When running external commands that are requiring user input, it is advised to run them in a non-interactive mode:
ExternalEvaluate is printing output in real time: