ShellProcess
DescriptionThe ShellProcess datatype represents a persistent shell (cmd.exe or PowerShell) and provides methods to execute commands by writing to stdin and reading from stdout/stderr.
It is designed for long-running shell sessions that can be interacted with programmatically.
You can write commands to the shell, read their output, and control the environment and working directory.
Typical use cases
Automating command-line tasks via a persistent shell session.
Interacting with console applications programmatically.
Reading stdout/stderr of shell commands.
Scripting and testing applications from a shell context.
Notes
- Commands are executed in an invisible console window.
- The shell session is persistent until terminated or killed.
- Environment variables can be set per shell session before executing commands.
- Stdout and stderr must be read explicitly using the provided read methods.
- Methods generally do not throw exceptions; errors are logged and indicated by return values.
------------------------------------------------------------
Methods
------------------------------------------------------------
bool start(string shellType="cmd")
Starts a persistent shell session.
shellType can be "cmd" or "powershell".
Returns true if the shell was successfully started.
------------------------------------------------------------
void writeStdIn(string text)
Writes text to the shell's standard input.
Use this to send commands to the running shell.
------------------------------------------------------------
string readStdOut()
Reads all currently available stdout from the shell.
Returns an empty string if no output is available.
------------------------------------------------------------
string readStdErr()
Reads all currently available stderr from the shell.
Returns an empty string if no error output is available.
------------------------------------------------------------
bool isRunning()
Returns true if the shell process is currently running.
------------------------------------------------------------
void terminate(bool graceful=true)
Terminates the shell session.
If graceful is true, attempts a clean shutdown by closing the shell.
If false, the shell is terminated immediately.
------------------------------------------------------------
void kill()
Forcibly kills the shell process immediately.
------------------------------------------------------------
void setEnvironment(string key, string value)
Sets an environment variable for the shell session.
The variable will be available to subsequently executed commands.
------------------------------------------------------------
void setWorkingDirectory(string path)
Sets the working directory for the shell session.
------------------------------------------------------------
Example
void main()
{
ShellProcess shell;
string output;
string err;
// Start CMD shell
if (!shell.start("cmd"))
{
print("Failed to start shell");
return;
}
// Set working directory and environment variable
shell.setWorkingDirectory("C:\\Temp");
shell.setEnvironment("MY_VAR", "123");
// Send a command to the shell
shell.writeStdIn("echo Hello World");
sleep(500);
output = shell.readStdOut();
print("Output:\n" + output);
// Send another command
shell.writeStdIn("dir");
sleep(500);
output = shell.readStdOut();
err = shell.readStdErr();
if (output != "")
print("Dir Output:\n" + output);
if (err != "")
print("Dir Error:\n" + err);
// Terminate shell
shell.terminate(true);
}