如何從 Go 程序執(zhí)行 bash 腳本?這是我的代碼:目錄結構:/hello/ public/ js/ hello.js templates hello.html hello.go hello.sh你好去cmd, err := exec.Command("/bin/sh", "hello.sh") if err != nil { fmt.Println(err)}當我運行 hello.go 并調用相關路由時,我在控制臺上得到了這個:exit status 127output is我期待 ["a", "b", "c"]我知道 SO: Executing a Bash Script from Golang上有一個類似的問題,但是,我不確定我的路徑是否正確。將不勝感激幫助!
3 回答

手掌心
TA貢獻1942條經(jīng)驗 獲得超3個贊
exec.Command() 返回一個可用于其他命令的結構,如 Run
如果您只是在尋找命令的輸出,請嘗試以下操作:
package main
import (
"fmt"
"log"
"os/exec"
)
func main() {
out, err := exec.Command("date").Output()
if err != nil {
log.Fatal(err)
}
fmt.Printf("The date is %s\n", out)
}

揚帆大魚
TA貢獻1799條經(jīng)驗 獲得超9個贊
您還可以使用 CombinedOutput() 而不是 Output()。它將轉儲執(zhí)行命令的標準錯誤結果,而不僅僅是返回錯誤代碼。
- 3 回答
- 0 關注
- 334 瀏覽
添加回答
舉報
0/150
提交
取消