可以使用os/exec包中的Command函数来调用执行.sh文件。

示例代码:

package main

import (
    "fmt"
    "os/exec"
)

func main() {
    cmd := exec.Command("/bin/bash", "path/to/script.sh")
    out, err := cmd.Output()
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(string(out))
}

在上面的代码中,我们使用exec包中的Command函数来创建一个执行bash命令的命令对象。我们将shell脚本的路径作为第二个参数传递给Command函数。接下来,我们使用Output方法来执行命令并获取命令输出。如果命令执行失败,我们将输出错误信息。最后,我们打印命令的输出。

如何用golang调用执行sh文件

原文地址: https://www.cveoy.top/t/topic/fIsN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录