介绍:平时有时候需要在go 执行 shell 脚本,参数传递的怎么传,下面是代码的实例
go文档cmd
package main
import (
"bytes"
"fmt"
"log"
"os/exec"
)
func main() {
exec_shell("uname ")
fmt.Println("hello world go 語言")
}
func exec_shell(s string) {
host := "127.0.0.1"
port := "9000"
userName := "root"
pwd := "123456"
command := "./test.sh "+host+" "+port+" "+ userName+" "+pwd//脚本的路径
cmd := exec.Command("/bin/bash", "-c",command)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s", out.String())
}
本作品采用《CC 协议》,转载必须注明作者和本文链接