exec - running other programs.
Run takes the program and its arguments as a list, no shell in between, and waits for it to finish:
exec = import("os/exec")r = exec.Run(["git", "rev-parse", "HEAD"])
if r.Status != 0 { ... }
println(r.Stdout)Output is the short way to the text of a program that is expected to work, and returns an error when it doesn't:
head = exec.Output(["git", "rev-parse", "HEAD"])The arguments go to the program as they are, so a file named "; rm -rf ." is a file name and nothing else. Sh runs a command line through /bin/sh when pipes and redirections are really wanted, and then the string is a program: don't build it out of anything that came from outside.
ponytail: the program is waited for, its output arrives at the end. Nothing runs in the background and nothing is read while it runs, which is what a long lived child would need.
¶MaxOutput = 1 << 20valuesource
MaxOutput is how much of the output of a program is kept by default, one megabyte. Options.Max raises it for a program that writes more.
¶Options = fn()source
Options returns the settings of a run, to change and pass to RunWith:
o = exec.Options()
o.Input = "text on the standard input"
o.Stderr = true # the error output is kept along with the output
o.Quiet = false # true lets the program write to the terminal
o.Max = 1 << 24 # keep up to sixteen megabytes
o.Dir = "/tmp" # run it there
¶Input = nullvaluesource
¶Stderr = falsevaluesource
¶Quiet = falsevaluesource
¶Max = MaxOutputvaluesource
¶Dir = nullvaluesource
¶RunWith = fn(argv, opts)source
RunWith runs argv with the given options and returns the result: Status is the exit status, Stdout what the program wrote, and Truncated whether more came out than Max allowed.
¶Argv = argvvaluesource
¶Status = statusvaluesource
¶Truncated = out != null && n > len(out)valuesource
¶Stdoutvaluesource
¶Run = fn(argv)source
Run runs argv, keeps what it writes, and returns the result. Only a program that could not be started at all is an error: one that ran and failed is a Status other than zero.
¶Argv = argvvaluesource
¶Status = statusvaluesource
¶Truncated = out != null && n > len(out)valuesource
¶Stdoutvaluesource
¶Output = fn(argv)source
Output returns what argv wrote on its standard output, and an error when it ended with a status other than zero.
¶Status = fn(argv)source
Status runs argv without keeping its output, which goes to the terminal, and returns the exit status.
¶Quiet = truevaluesource
¶Sh = fn(cmd)source
Sh runs a command line through /bin/sh, pipes, redirections and all. The command is a program, so keep anything that came from outside out of it, or pass it in Options.Input.
¶Argv = argvvaluesource
¶Status = statusvaluesource
¶Truncated = out != null && n > len(out)valuesource
¶Stdoutvaluesource
¶Look = fn(name)source
Look returns the path of the program name, searched for in PATH the way a shell would, or an error when there is no such program.