我试图找到一种方法来根据同一会话中上一个命令的输出运行一些命令。
例如:
cmd_one
,cmd_two
和cmd_three
。cmd_one
。cmd_one
返回非零退出状态,则运行cmd_two
,cmd_three
。这样的事情:
package main
import (
"fmt"
"os/exec"
)
func main() {
cmd := exec.Command("/bin/sh")
pw, _ := cmd.StdinPipe()
pr, _ := cmd.StdoutPipe()
pe, _ := cmd.StderrPipe()
cmd.Start()
// run "cmd_one"
pw.Write([]byte("cmd_one\n"))
/*
Some way to get the output of "cmd_one" <- Tricky one
if "cmd_one" exits with non-zero exit status, run "cmd_two"
else run "cmd_three"
*/
pw.Close()
cmd.Wait()
}