os/shell
Running a command and getting its exit code, its output and its errors together.
import os.shell
The os.shell package runs a command and gives back the whole result at once: the exit code, the output and the error output.
Exec splits the command on spaces alone and no shell is involved: pipes (|), redirection (>) and quoting do not work. For those, call sh -c through Run, as the example does.
Types
CmdResult
struct CmdResultThe result of a command.
| Name | Type | Description |
|---|---|---|
code | int = 0 | The exit code; zero means success. |
out | str = "" | The standard output. |
err | str = "" | The error output. |
import os.shell
func main:
mut args := Vector {} as Vector<str>
defer args.free()
args.push("-n")
args.push("hello")
r := shell.Run("echo", args)
println r.code, "[" + r.out + "]"
r2 := shell.Exec("echo one two three")
println r2.code, "[" + r2.out + "]"
mut args2 := Vector {} as Vector<str>
defer args2.free()
args2.push("-c")
args2.push("echo oops >&2; exit 3")
r3 := shell.Run("sh", args2)
println r3.code, "[" + r3.err + "]"
println shell.System("true"), shell.System("false") != 0
endOutput0 [hello]
0 [one two three
]
3 [oops
]
0 true
Functions
Run
func Run(command: str, args: Vector<str>): CmdResultRuns a program with its arguments given separately and waits for it to finish.
The example for this item is shown with the ones above: os-shell-Run.salam
Exec
func Exec(command: str): CmdResultSplits a command string on spaces into a program and its arguments and runs it, with no shell.
The example for this item is shown with the ones above: os-shell-Run.salam
System
func System(command: str): intRuns a command in the real shell (
/bin/sh or cmd) and returns only the exit code; the output is printed directly.The example for this item is shown with the ones above: os-shell-Run.salam