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.

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



  • ¥15 如何查看h3c 5130s-28f-si交换机配置 及更改网口
  • ¥15 HFSS仿真喇叭天线阵列方向图出现异常
  • ¥15 blender自发光强度
  • ¥15 FIR滤波器算法设计与实现(利用库函数firdec()
  • ¥15 利用单片机产生正弦信号,来测量差分放大电路放大倍数和共模放大倍数的思路和仿真
  • ¥60 关于mavlink的论文咨询
  • ¥66 定制开发肯德基自动化网站下单软件
  • ¥15 中科院研发的TextMind(文心)
  • ¥20 vscode虚拟环境依赖包未安装
  • ¥15 odoo17关于owl开发js代码问题