Standard library · System and tools

os/shell

Running a command and getting its exit code, its output and its errors together.

import os.shell
On this page
  1. Types
  2. Functions

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 CmdResult
The result of a command.
NameTypeDescription
codeint = 0The exit code; zero means success.
outstr = ""The standard output.
errstr = ""The error output.
os-shell-Run.salam
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
end
Output0 [hello] 0 [one two three ] 3 [oops ] 0 true

Functions

Run

func Run(command: str, args: Vector<str>): CmdResult
Runs 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): CmdResult
Splits 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): int
Runs 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