1.方法代码:
func shellExec(cmdStr string) (string , error){
cmd := exec.Command("bash","-c",cmdStr)
strOut , strErr := bytes.Buffer{} , bytes.Buffer{}
cmd.Stdout = &strOut
cmd.Stderr = &strErr
err := cmd.Run()
if err != nil {
return strErr.String() , err
}
return strOut.String() , err
}
2.示例代码:
func main(){
res , err := shellExec("ls")
fmt.Println(res , err)
}
网友评论