dongyumiao5210 2015-12-24 23:01
浏览 495
已采纳

在同一shell golang中运行多个Exec命令

I'm having trouble figuring out how to run multiple commands using the os/exec package. I've trolled the net and stackoverflow and haven't found anything that works for me case. Here's my source:

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
}

I want to the command like I would from terminal. cd path; ./configure; make So I need run each command in order and wait for the last command to finish before moving on. With my current version of the code it currently says that ./configure: no such file or directory I assume that is because cd path executes and in a new shell ./configure executes, instead of being in the same directory from the previous command. Any ideas? UPDATE I solved the issue by changing the working directory and then executing the ./configure and make command

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

Still now i'm curious to know if there is a way to execute commands in the same shell.

  • 写回答

1条回答 默认 最新

  • duanjue2576 2015-12-25 00:20
    关注

    If you want to run multiple commands within a single shell instance, you will need to invoke the shell with something like this:

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

    This will get the shell to interpret the given commands. It will also let you execute shell builtins like cd. Note that this can be non-trivial to substitute in user data to these commands in a safe way.

    If instead you just want to run a command in a particular directory, you can do that without the shell. You can set the current working directory to execute the command like so:

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

    ... and continue on like you were before.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果