介紹:平時有時候需要在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 協議》,轉載必須註明作者和本文連結