Cmd.read
func Cmd.read(c: Cmd | Cmd.Pipeline, input: string | Bytes = "", ctx: Context | null = null): Result[string, Error]Cmd.read runs c with input as its standard input, waits for it to exit, and returns its standard output as text without one final newline. A nonzero status is Err with the message "program: exit status n", followed by ": " and standard error when it wrote any; program is that of the stage that set the status, with "(stage i)" after it when another stage runs the same program. Err also means a stage could not be started or ctx was done, which kills every stage.
Details
Cmd.readremoves one final\nor\r\nfrom the output, as a shell's$(...)does, and keeps all other white space.Cmd.readBytesreturns the output exactly.- Standard error is collected, not shown. It appears only in the message of an
Err, with its trailing white space removed. - Any nonzero status is
Err. For a program whose nonzero status is an answer, such asgrepwith no match, useCmd.capture. - No shell runs the command, so
$HOME,*.txt, and|inargsreach the program as they are. Join commands withCmd.pipe. - A string
inputis given as its UTF-8, andBytesas they are. The default""gives the command an empty standard input, never the program's own.
Basic examples
Run a program and get what it prints:
let greeting = try Cmd.read(Cmd.new("echo", ["hello", "world"]))
println(greeting)hello worldGive the command standard input:
let loud = try Cmd.read(Cmd.new("tr", ["a-z", "A-Z"]), "quiet please")
println(loud)QUIET PLEASEA nonzero status is Err, with what the command wrote to standard error:
println(Cmd.read(Cmd.new("sh", ["-c", "echo 'disk full' >&2; exit 3"])))Err{ error: Error{ message: "sh: exit status 3: disk full" } }Scope
Only one final line break goes. Other line breaks, and spaces, stay:
println(Cmd.read(Cmd.new("printf", ["a\nb\n\n"])))
println(Cmd.read(Cmd.new("printf", [" 42 \r\n"])))Ok{ value: "a\nb\n" }
Ok{ value: " 42 " }A command that prints nothing gives "":
println(Cmd.read(Cmd.new("true")))Ok{ value: "" }Input of several lines reaches the command as it is:
let out = try Cmd.read(Cmd.new("sort"), "pear\napple\nfig\n")
println(out.lines())["apple", "fig", "pear"]Bytes as input reach the command as they are:
let input = Bytes.fromArray([0x41, 0x6e, 0x74, 0x0a])
println(try Cmd.read(Cmd.new("cat"), input))AntA command that writes to standard error and exits with status 0 is Ok. Its standard error is dropped:
println(Cmd.read(Cmd.new("sh", ["-c", "echo result; echo 'warning: slow' >&2"])))Ok{ value: "result" }The dir and env of the Cmd set where the command runs and what environment it sees:
println(try Cmd.read(Cmd.new("pwd", dir: "/")))
let env = Map{ NAME: "Ant" }
println(try Cmd.read(Cmd.new("sh", ["-c", `echo "hi $NAME"`], env: env)))/
hi AntA pipeline reads input in its first stage and returns the output of its last:
let words = "b\na\nb\nc\na\n"
let unique = try Cmd.read(Cmd.pipe([Cmd.new("sort"), Cmd.new("uniq")]), words)
println(unique.lines())["a", "b", "c"]The status of a pipeline is that of its rightmost stage that failed. When another stage runs the same program, the message names the stage, counting from 1:
let pipeline = Cmd.pipe([
Cmd.new("printf", ["ant\nbee\n"]),
Cmd.new("grep", ["a"]),
Cmd.new("grep", ["z"]),
])
println(Cmd.read(pipeline))Err{ error: Error{ message: "grep (stage 3): exit status 1" } }A stage that ends because a later stage stopped reading does not fail the pipeline:
println(Cmd.read(Cmd.pipe([Cmd.new("yes"), Cmd.new("head", ["-n", "2"])])))Ok{ value: "y\ny" }A program that cannot be started is Err too:
println(Cmd.read(Cmd.new("no-such-program")))
println(Cmd.read(Cmd.new("ls", dir: "/no/such/dir")))Err{ error: Error{ message: "exec: \"no-such-program\": executable file not found in $PATH" } }
Err{ error: Error{ message: "chdir /no/such/dir: no such file or directory" } }Named arguments
input
input: names the standard input, which reads well next to the other arguments:
let first = try Cmd.read(Cmd.new("head", ["-n", "1"]), input: "one\ntwo\nthree\n")
println(first)onectx
When ctx is done while the command runs, every stage is killed and the result is Err with the context's reason:
let ctx = Context.new(50ms)
println(Cmd.read(Cmd.new("sleep", ["10"]), ctx: ctx))Err{ error: Error{ message: "timeout" } }A context that is already done starts nothing:
let ctx = Context.new()
ctx.cancel()
println(Cmd.read(Cmd.new("echo", ["never"]), ctx: ctx))Err{ error: Error{ message: "cancelled" } }Applications
Ask a program for an answer only it has. git gives the id it would store a blob under:
let id = try Cmd.read(Cmd.new("git", ["hash-object", "--stdin"]), "hello\n")
println(id)ce013625030ba8dba906f756967f9e9ca394464aWrap a command in a function that names the task in its error, so the caller sees what failed:
func lineCount(paths: Array[string]): Result[int, Error] {
let count = Cmd.pipe([Cmd.new("cat", paths), Cmd.new("wc", ["-l"])])
let out = try (Cmd.read(count) | Result.wrapErr("count lines"))
Result.ok(out.trim().toInt() ?? 0)
}
try File.write("a.txt", "one\ntwo\n")
try File.write("b.txt", "three\n")
println(lineCount(["a.txt", "b.txt"]))
println(lineCount(["a.txt", "missing.txt"]))Ok{ value: 3 }
Err{ error: Error{ message: "count lines: cat: exit status 1: cat: missing.txt: No such file or directory" } }Run a short shell script when you need the shell's features, passing values as arguments, not by building the script text:
let script = `for f in "$@"; do echo "[$f]"; done`
let files = ["notes.txt", "my file.txt"]
let out = try Cmd.read(Cmd.new("sh", ["-c", script, "sh", ...files]))
println(out)[notes.txt]
[my file.txt]Properties and relations
Cmd.read gives the stdout of Cmd.capture without its final line break:
let c = Cmd.new("printf", ["one\ntwo\n"])
let out = try Cmd.capture(c)
assert(try Cmd.read(c) == out.stdout.trimSuffix("\n"))Cmd.readBytes keeps the line break that Cmd.read removes:
let c = Cmd.new("echo", ["hi"])
let bytes = try Cmd.readBytes(c)
assert(String.fromBytes(bytes) == "${try Cmd.read(c)}\n")Possible issues
No shell runs the command. A whole command line as program names a program that does not exist, and $HOME or * in args is not expanded:
println(Cmd.read(Cmd.new("echo hello")))
println(try Cmd.read(Cmd.new("echo", ["$HOME", "*.txt"])))Err{ error: Error{ message: "exec: \"echo hello\": executable file not found in $PATH" } }
$HOME *.txtA program that reports an answer with its status fails Cmd.read. grep exits with 1 when nothing matches; Cmd.capture gives the status as a value:
let c = Cmd.new("grep", ["panic"])
println(Cmd.read(c, "all good\n"))
let out = try Cmd.capture(c, "all good\n")
println(out.status)Err{ error: Error{ message: "grep: exit status 1" } }
1env is the child's whole environment, not variables added to the program's own. With only NAME, the child has no HOME, and no PATH either. Start from Os.environ(), a new map at each call, and set the variable on it:
let script = `echo "HOME=$HOME"`
println(try Cmd.read(Cmd.new("sh", ["-c", script], env: Map{ NAME: "Ant" })))
let env = Os.environ()
env["NAME"] = "Ant"
println(try Cmd.read(Cmd.new("sh", ["-c", script], env: env)))HOME=
HOME=/home/antPadding stays in the output, so trim it before parsing a number:
let out = try Cmd.read(Cmd.new("printf", ["%8d\n", "42"]))
println(out.toInt())
println(out.trim().toInt())null
42See also
Cmd.readBytes · Cmd.capture · Cmd.run · Cmd.start · Cmd.new · Cmd.pipe · Os.which
