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"

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

    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)
    cd := exec.Command("cd", folderPath)
    config := exec.Command("./configure", "--disable-yasm")
    build := exec.Command("make")

    cd_err := cd.Start()
    if cd_err != nil {
    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.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.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")

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.

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



  • ¥55 AD844 howland电流源如何驱动大额负载
  • ¥15 C++ /QT 内存权限的判断函数列举
  • ¥15 深度学习GFnet理解问题
  • ¥15 单细胞小提琴堆叠图代码
  • ¥50 升级strust2版本到2.3.15.1后使用ognl3.0.6.jar windows环境中没有问题,但部署到linux环境报错
  • ¥15 vue页面,node封装接口
  • ¥15 求TMS320F280039C工程模板!
  • ¥15 delphi+fastreport实现分组补空打印问题
  • ¥15 使用python把两台mysql数据库服务器数据导出和导入
  • ¥15 NodeBB论坛配置Apache Solr中文搜索引擎的详细教程