Golang - 执行shell脚本,实时输出shell脚本中的日志:
package main
import (
"bufio"
"fmt"
"io"
"os/exec"
"strings"
)
func SafetyShell(execName string, arg ...string) (string, error) {
ex := exec.Command(execName, arg...)
result, err := ex.CombinedOutput()
if err != nil {
return "", err
}
return strings.TrimSpace(string(result)), nil
}
func execSH() bool {
execSH := []string{"./exec.sh"}
res, err := SafetyShell("/bin/sh", execSH...)
fmt.Println(fmt.Sprintf("res: %+v", res))
fmt.Println(fmt.Sprintf("err: %+v", err))
if err != nil {
return false
}
return true
}
func executeShellScript(execName string, arg ...string) error {
cmd := exec.Command(execName, arg...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
if