Shell
Class Shell
The Shell class provides simple access to the Unix/Linux or Windows shell. You can execute a shell command, and then retrieve the result and/or error code.
The following methods require a Shell instance:
| Method Prototype | Description
|
| Sub Constructor() | initialize the Shell object
|
| Dim SafeMode As Boolean | get/set whether arguments should be escaped (defaults to True)
|
| Function ErrorCode() As Integer | get the shell process error code, if any
|
| Sub Execute(command As String, arguments As String="") | execute the given command (optionally, with arguments)
|
| Function Result() As String | get the result of the last Execute call
|
| Function TimeOut() As Integer | get the number of seconds the shell will wait before timing out
|
| Sub TimeOut(Assigns newValue As Integer) | set the number of seconds the shell will wait before timing out
|
The following shared methods can be called without a specific instance, just using the "Shell." prefix:
| Method Prototype | Description
|
| Shared Function EscapeArgument(arg as String) As String | escape an argument for shell security
|
| Shared Function EscapeCmd(command as String) As String | escape a command for shell security
|
| Shared Function Execute(command As String, arguments As String="") As String | execute a shell command (in safe mode) and get the result
|
Yuma's Shell class supports a "safe mode," which escapes special characters from the command or arguments before passing them onto the shell. This is an important security feature, as if malicious users find any way to inject those characters into a shell call (for example, because your web app prompts them for a file name and then passes that on to a shell command), they might be able to execute arbitrary shell commands. So it is highly recommended to leave SafeMode set to True.
For simple needs, you don't actually need to instantiate Shell at all; just call Shell.Execute instead, which returns the result as a string. If you need more control over the call, such as adjusting the TimeOut value, avoiding safe mode, or retrieving the error code, then create a Shell object and use that instead.