Cmd.capture
func Cmd.capture(c: Cmd | Cmd.Pipeline, input: string | Bytes = "", ctx: Context | null = null): Result[Cmd.Output, Error]Cmd.capture runs c with input as its standard input, waits for it to exit, and captures its status and outputs. A nonzero status is Ok; Err means a stage could not be started or run to its end, and when ctx is done every stage is killed and the result is Err with ctx's reason.
Details
- The result is
Errfor a program that is not found or adirthat does not exist. stdoutandstderrhold the whole output as text, final newline included. Bytes that are not UTF-8 become U+FFFD in them; Cmd.captureBytes keeps standard output as the command wrote it.inputis what the first stage reads: a string as its UTF-8,Bytesas they are. By default the command reads nothing.- For a pipeline,
statusis that of the rightmost stage that failed, or 0, andstatusesholds each stage's own. A stage before the last that SIGPIPE ended does not count, thoughstatusesstill shows its 141. The stages' standard error interleaves as it arrives, in no promised order. - Where the host has signals, a command ended by signal
nhas the status128 + n, as a shell reports it. On Windows the status is the exit code.
Basic examples
Run a command and read its status and both outputs. The exit status 2 is Ok:
let script = "echo built; echo 'warning: slow' >&2; exit 2"
let out = try Cmd.capture(Cmd.new("sh", ["-c", script]))
println(out.status)
print(out.stdout)
print(out.stderr)2
built
warning: slowGive the command its standard input as a string:
let out = try Cmd.capture(Cmd.new("sort"), "pear\napple\nfig\n")
print(out.stdout)apple
fig
pearScope
The whole Cmd.Output record, for a command that writes to both streams:
let out = try Cmd.capture(Cmd.new("sh", ["-c", "echo hi; echo oops >&2"]))
println(out)Cmd.Output{ status: 0, statuses: [0], stdout: "hi\n", stderr: "oops\n" }input as Bytes reaches the command unchanged, bytes that are not text included:
let data = Bytes.fromArray([0, 1, 254, 255])
let out = try Cmd.capture(Cmd.new("wc", ["-c"]), data)
println(out.stdout.trim())4Output that is not UTF-8 has U+FFFD in stdout; Cmd.captureBytes gives its bytes:
let c = Cmd.new("printf", ["\\377\\n"])
println((try Cmd.capture(c)).stdout == "\u{FFFD}\n")
println((try Cmd.captureBytes(c)).stdout)true
Bytes(ff 0a)A pipeline from Cmd.pipe is captured as one command. input goes to the first stage, and the last stage's output is stdout:
let words = Cmd.pipe([Cmd.new("tr", ["a-z", "A-Z"]), Cmd.new("sort", ["-u"])])
let out = try Cmd.capture(words, "bee\nant\nbee\n")
print(out.stdout)
println(out.statuses)ANT
BEE
[0, 0]The status of a pipeline is that of the rightmost stage that failed. A first stage that SIGPIPE ended because a later stage stopped reading does not count, but statuses still shows it:
let failed = try Cmd.capture(Cmd.pipe([Cmd.new("false"), Cmd.new("cat")]))
println("${failed.status} ${failed.statuses}")
let first = try Cmd.capture(Cmd.pipe([Cmd.new("yes"), Cmd.new("head", ["-n", "1"])]))
println("${first.status} ${first.statuses}")1 [1, 0]
0 [141, 0]A command's dir and env apply as they do for every function that runs it. env is the child's whole environment:
let script = "echo \"$GREETING from $(pwd)\""
let c = Cmd.new("sh", ["-c", script], dir: "/", env: Map{ "GREETING": "hello" })
let out = try Cmd.capture(c)
print(out.stdout)hello from /A command that a signal ends has the status 128 + n, here 137 for SIGKILL:
let out = try Cmd.capture(Cmd.new("sh", ["-c", "kill -9 $$"]))
println(out.status)137Named arguments
input: names the standard input, which reads well when the command is built inline:
let out = try Cmd.capture(Cmd.new("wc", ["-l"]), input: "one\ntwo\nthree\n")
println(out.stdout.trim())3ctx: bounds the run. When the context is done first, every stage is killed and the result is Err with the context's reason:
let ctx = Context.new(100ms)
println(Cmd.capture(Cmd.new("sleep", ["5"]), ctx: ctx))Err{ error: Error{ message: "timeout" } }Applications
grep exits with 1 when nothing matches and with 2 on an error. Cmd.capture lets a program tell the three apart, where Cmd.read would fail on both 1 and 2:
func countMatches(pattern: string, text: string): Result[int, Error] {
let out = try Cmd.capture(Cmd.new("grep", ["-c", pattern]), text)
if out.status > 1 {
return Result.fail("grep: ${out.stderr.trim()}")
}
Result.ok(out.stdout.trim().toInt() ?? 0)
}
let log = "ok\nerror: disk\nok\nerror: net\n"
println(try countMatches("error", log))
println(try countMatches("panic", log))2
0Run a list of checks, report each one, and show the standard error of those that failed:
let checks = [
Cmd.new("true"),
Cmd.new("sh", ["-c", "echo 'missing LICENSE' >&2; exit 1"]),
Cmd.new("test", ["-d", "/"]),
]
var failures = 0
for c in checks {
let out = try Cmd.capture(c)
if out.status == 0 {
println("ok ${Cmd.string(c)}")
continue
}
failures += 1
println("FAIL ${Cmd.string(c)}: ${out.stderr.trim()}")
}
println("${failures} failed")ok true
FAIL sh -c 'echo '\''missing LICENSE'\'' >&2; exit 1': missing LICENSE
ok test -d /
1 failedProperties and relations
For output that is UTF-8, stdout is what Cmd.captureBytes gives, read as text:
let c = Cmd.new("echo", ["héllo"])
let raw = try Cmd.captureBytes(c)
assert(String.fromBytes(raw.stdout) == (try Cmd.capture(c)).stdout)
assert(raw.stdout.length() == 7)On status 0, Cmd.read returns stdout without its final newline:
let c = Cmd.new("printf", ["a\\nb\\n"])
let out = try Cmd.capture(c)
assert(out.status == 0)
assert(try Cmd.read(c) == out.stdout.trimSuffix("\n"))For a single command, statuses holds one status, the same as status:
let out = try Cmd.capture(Cmd.new("sh", ["-c", "exit 5"]))
assert(out.statuses == [out.status])
assert(out.status == 5)Possible issues
try passes a failed command through: a nonzero status is Ok. Check status, or use Cmd.read when a nonzero status should be Err:
let out = try Cmd.capture(Cmd.new("ls", ["/no/such/file"]))
println(out.status == 0)
println(Cmd.read(Cmd.new("false")))false
Err{ error: Error{ message: "false: exit status 1" } }A program that is not found is Err, not the status 127 a shell gives. Only a shell started by the command reports 127:
println(Cmd.capture(Cmd.new("no-such-program")))
let out = try Cmd.capture(Cmd.new("sh", ["-c", "no-such-program"]))
println(out.status)Err{ error: Error{ message: "exec: \"no-such-program\": executable file not found in $PATH" } }
127env replaces the child's whole environment, so a command given one variable loses HOME, PATH, and every other. To add a variable, start from Os.environ:
let script = "echo \"$GREETING, home is '$HOME'\""
let bare = try Cmd.capture(Cmd.new("sh", ["-c", script], env: Map{ "GREETING": "hi" }))
print(bare.stdout)
let env = Os.environ()
env.set("GREETING", "hi")
let full = try Cmd.capture(Cmd.new("sh", ["-c", script], env: env))
println(full.stdout.trim() == "hi, home is '${Os.env("HOME") ?? ""}'")hi, home is ''
truestdout keeps the final newline the command wrote, so a comparison with the bare value fails. Trim it, or use Cmd.read:
let out = try Cmd.capture(Cmd.new("echo", ["yes"]))
println(out.stdout == "yes")
println(out.stdout.trimEnd() == "yes")false
trueCmd.capture holds the whole output in memory and returns only once the command exits. To read output as it comes, or more than fits in memory, use Cmd.start with a pipe from Os.openPipe.
See also
Cmd.read · Cmd.readBytes · Cmd.run · Cmd.start · Cmd.pipe · Cmd.new · Cmd.string · Cmd.Output · Os.openPipe
