Documentation
¶
Overview ¶
Example ¶
package main import ( "fmt" "log" "catinello.eu/exec" ) func main() { o, _, c := exec.Simple("echo '!olleH' | rev") if c != 0 { log.Fatal("Unexpected failure.") } fmt.Println(o) }
Output: Hello!
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func Fork ¶
Fork takes your cmd and returns the pid of the started process.
Example ¶
package main import ( "fmt" "log" "catinello.eu/exec" ) func main() { pid, err := exec.Fork("sleep 1", nil) if err != nil { log.Fatal("Unexpected failure.") } if pid > 1 { fmt.Println("success") } else { fmt.Println("failure") } }
Output: success
func Run ¶
Run takes your cmdline and executes it.
Example ¶
package main import ( "fmt" "log" "catinello.eu/exec" ) func main() { stdout, stderr, codes, err := exec.Run(`echo "Hello world!"; python3 -c 'import sys; print("error", file=sys.stderr)' && false || echo "success"`) if err != nil { log.Fatal(err) } for n, code := range codes { // skip the third command (false) that returns 1 if code != 0 && n != 2 { log.Fatal(stderr[n]) } } // the python command prints error to stderr if codes[1] == 0 { if stderr[1] != "error\n" { log.Fatal("Stderr output missing.") } } fmt.Println(stdout[0]) }
Output: Hello world!
func RunWithTokens ¶
RunWithTokens takes your cmdline tokens and does not try to tokenize before executing it.
Example ¶
package main import ( "fmt" "log" "catinello.eu/exec" ) func main() { stdout, stderr, codes, err := exec.RunWithTokens([]string{"echo", "Hello world!", ";", "python3", "-c", `'import sys; print("error", file=sys.stderr)'`, "&&", "false", "||", "echo", "success"}) if err != nil { log.Fatal(err) } for n, code := range codes { // skip the third command (false) that returns 1 if code != 0 && n != 2 { log.Fatal(stderr[n]) } } // the python command prints error to stderr if codes[1] == 0 { if stderr[1] != "error\n" { log.Fatal("Stderr output missing.") } } fmt.Println(stdout[0]) }
Output: Hello world!
func Simple ¶
Simple takes your cmdline and executes it.
Example ¶
package main import ( "fmt" "log" "catinello.eu/exec" ) func main() { stdout, stderr, code := exec.Simple(`echo "Hello world!"; python3 -c 'import sys; print("error", file=sys.stderr)' && false || echo "success"`) if code == -1 { log.Fatal(stderr) } if code > 0 { log.Fatal("Unexpected failure.") } if code == 0 && stderr != "error\n" { log.Fatal("Stderr output missing.") } fmt.Println(stdout) }
Output: Hello world! success
Types ¶
This section is empty.
Click to show internal directories.
Click to hide internal directories.