【问题标题】:Run Multiple Exec Commands in the same shell golang在同一个 shell golang 中运行多个 Exec 命令
【发布时间】:2015-12-24 23:01:13
【问题描述】:

我无法弄清楚如何使用 os/exec 包运行多个命令。我已经浏览了网络和 stackoverflow,但没有找到任何适合我的案例。这是我的来源:

package main

import (
    _ "bufio"
    _ "bytes"
    _ "errors"
    "fmt"
    "log"
    "os"
    "os/exec"
    "path/filepath"
)

func main() {
    ffmpegFolderName := "ffmpeg-2.8.4"
    path, err := filepath.Abs("")
    if err != nil {
        fmt.Println("Error locating absulte file paths")
        os.Exit(1)
    }

    folderPath := filepath.Join(path, ffmpegFolderName)

    _, err2 := folderExists(folderPath)
    if err2 != nil {
        fmt.Println("The folder: %s either does not exist or is not in the same directory as make.go", folderPath)
        os.Exit(1)
    }
    cd := exec.Command("cd", folderPath)
    config := exec.Command("./configure", "--disable-yasm")
    build := exec.Command("make")

    cd_err := cd.Start()
    if cd_err != nil {
        log.Fatal(cd_err)
    }
    log.Printf("Waiting for command to finish...")
    cd_err = cd.Wait()
    log.Printf("Command finished with error: %v", cd_err)

    start_err := config.Start()
    if start_err != nil {
        log.Fatal(start_err)
    }
    log.Printf("Waiting for command to finish...")
    start_err = config.Wait()
    log.Printf("Command finished with error: %v", start_err)

    build_err := build.Start()
    if build_err != nil {
        log.Fatal(build_err)
    }
    log.Printf("Waiting for command to finish...")
    build_err = build.Wait()
    log.Printf("Command finished with error: %v", build_err)

}

func folderExists(path string) (bool, error) {
    _, err := os.Stat(path)
    if err == nil {
        return true, nil
    }
    if os.IsNotExist(err) {
        return false, nil
    }
    return true, err
}

我想像从终端一样执行命令。 cd path; ./configure; make 所以我需要按顺序运行每个命令并等待最后一个命令完成,然后再继续。使用我当前版本的代码,它目前说./configure: no such file or directory 我认为这是因为 cd 路径执行并且在新的 shell 中执行 ./configure,而不是与上一个命令位于同一目录中。有任何想法吗? 更新我通过更改工作目录然后执行 ./configure 和 make 命令解决了这个问题

err = os.Chdir(folderPath)
    if err != nil {
        fmt.Println("File Path Could not be changed")
        os.Exit(1)
    }

现在我很想知道是否有办法在同一个 shell 中执行命令。

【问题讨论】:

    标签: shell command-line go exec


    【解决方案1】:

    如果您想在单个 shell 实例中运行多个命令,则需要使用以下方式调用 shell:

    cmd := exec.Command("/bin/sh", "-c", "command1; command2; command3; ...")
    err := cmd.Run()
    

    这将使 shell 解释给定的命令。它还可以让你执行像cd 这样的shell 内置函数。请注意,以安全的方式将用户数据替换为这些命令并非易事。

    如果您只想在特定目录中运行命令,则可以不使用 shell。您可以设置当前工作目录来执行命令,如下所示:

    config := exec.Command("./configure", "--disable-yasm")
    config.Dir = folderPath
    build := exec.Command("make")
    build.Dir = folderPath
    

    ...像以前一样继续前进。

    【讨论】:

    • 感谢您的回答。我特别感谢你的第一个建议。我偶然发现了您的第二个建议,目前正在使用它。
    • @James 我尝试了您建议的方式。 command := exec.Command("echo *tar.gz | xargs -n1 tar zxf") command.Dir = pathFinal cmdErr := command.Run() 另一方面,这对我不起作用, command := "cd " +pathFinal+"; "+"echo *tar.gz | xargs -n1 tar zxf" cmd := exec.Command("/bin/sh", "-c", command) 这是有效的。我想以第一种方式实现它。我不知道为什么它不起作用。
    • 它抛出错误:无法解压缩文件:exec:“echo *tar.gz | xargs -n1 tar zxf”:$PATH 中找不到可执行文件
    • @supriya:glob 和管道是外壳功能,而不是内核解释的东西。你要么需要从 Go 中完成这些部分,要么显式调用 shell 并让它为你解释事情。您可以通过第一个命令的StdoutPipe() 将命令连接到第二个命令的Stdin
    猜你喜欢
    • 2017-10-12
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    相关资源
    最近更新 更多