如何从我的 Go 程序执行 bash 脚本?这是我的代码:
目录结构:
/hello/
public/
js/
hello.js
templates
hello.html
hello.go
hello.sh
你好.go
cmd, err := exec.Command("/bin/sh", "hello.sh")
if err != nil {
fmt.Println(err)
}
当我运行 hello.go 并调用相关路由时,我在控制台上看到了这个:
退出状态 127
输出是
我期待 ["a", "b", "c"]
我知道在 SO 上有一个类似的问题:Executing a Bash Script from Golang ,但是,我不确定我的路径是否正确。将不胜感激!
最佳答案
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)
}
关于bash - Go: 执行 bash 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27765143/