在Go语言中,在同一个shell中运行多个exec命令。

huangapple go评论79阅读模式
英文:

Run Multiple Exec Commands in the same shell golang

问题

我在使用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.Printf("The folder: %s either does not exist or is not in the same directory as make.go\n", 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 path执行后,在一个新的shell中执行./configure,而不是在前一个命令的同一目录中执行。有什么想法吗?
更新 我通过更改工作目录并执行./configuremake命令来解决了这个问题。

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

但我仍然很好奇是否有一种方法可以在同一个shell中执行命令。

英文:

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

得分: 41

如果你想在单个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

...然后继续你之前的操作。

英文:

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.

huangapple
  • 本文由 发表于 2015年12月25日 07:01:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/34458625.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定