本文主要是介绍go语言将cmd stdout和stderr作为字符串返回而不是打印到控制台,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
go语言将cmd stdout和stderr作为字符串返回而不是打印到控制台
1、直接打印到控制台
从 golang 应用程序中执行 bash 命令,现在 stdout 和 stderr 直接进入控制台:
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
package mainimport ("fmt""log""os""os/exec""time"
)func main() {ok, outString, errString := runBashCommandAndKillIfTooSlow("dir", 2000)fmt.Println("ok")fmt.Println(ok)fmt.Println("outString")fmt.Println(outString)fmt.Println("errString")fmt.Println(errString)
}/*run bash command and kill it if it works longer than "killInMilliSeconds" milliseconds
*/
func runBashCommandAndKillIfTooSlow(command string, killInMilliSeconds time.Duration) (okResult bool, stdout, stderr string) {fmt.Println("running bash command...")fmt.Println(command)// Linux// cmd := exec.Command("sh", "-c", command)// Windowscmd := exec.Command("cmd", "/C", command)cmd.Stdout = os.Stdout // cmd.Stdout -> stdoutcmd.Stderr = os.Stderr // cmd.Stderr -> stderrokResult = trueerr := cmd.Start()log.Printf("Waiting for command to finish...")done := make(chan error, 1)go func() {done <- cmd.Wait()}()select {case <-time.After(killInMilliSeconds * time.Millisecond):if err := cmd.Process.Kill(); err != nil {log.Fatal("failed to kill: ", err)okResult = false}// allow goroutine to exit<-donelog.Println("process killed")case err := <-done:if err != nil {log.Printf("process done with error = %v", err)okResult = false}}if err != nil {log.Fatal(err)okResult = false}return
}
如果 bash 命令太慢( killInMilliSeconds 参数),程序应该保持其终止 bash 命令的能力。
希望 stdout 和 stderr 作为字符串变量从 runBashCommandAndKillIfTooSlow 函数返回,而不立即打印到控
制台,如何实现。
2、不打印到控制台
将输出设置为 strings.Builder 或 bytes.Buffer:
var outbuf, errbuf strings.Builder // or bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
运行命令后,您可以通过调用 Builder.String() 获取字符串形式的标准输出和标准错误:
stdout := outbuf.String()
stderr := errbuf.String()
package mainimport ("fmt""log""os/exec""strings""time"
)func main() {ok, outString, errString := runBashCommandAndKillIfTooSlow("dir", 2000)fmt.Println("ok")fmt.Println(ok)fmt.Println("outString")fmt.Println(outString)fmt.Println("errString")fmt.Println(errString)
}/*run bash command and kill it if it works longer than "killInMilliSeconds" milliseconds
*/
func runBashCommandAndKillIfTooSlow(command string, killInMilliSeconds time.Duration) (okResult bool, stdout, stderr string) {fmt.Println("running bash command...")fmt.Println(command)// Linux// cmd := exec.Command("sh", "-c", command)// Windowscmd := exec.Command("cmd", "/C", command)var outBuf, errBuf strings.Builder // or bytes.Buffercmd.Stdout = &outBufcmd.Stderr = &errBufokResult = trueerr := cmd.Start()log.Printf("Waiting for command to finish...")done := make(chan error, 1)go func() {done <- cmd.Wait()}()select {case <-time.After(killInMilliSeconds * time.Millisecond):if err := cmd.Process.Kill(); err != nil {log.Fatal("failed to kill: ", err)okResult = false}// allow goroutine to exit<-donelog.Println("process killed")case err := <-done:if err != nil {log.Printf("process done with error = %v", err)okResult = false}}if err != nil {log.Fatal(err)okResult = false}stdout = outBuf.String()stderr = errBuf.String()return
}
这篇关于go语言将cmd stdout和stderr作为字符串返回而不是打印到控制台的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!