在同一shell golang中运行多个Exec命令 [英] Run Multiple Exec Commands in the same shell golang
问题描述
我在弄清楚如何使用os / exec软件包运行多个命令时遇到了麻烦。我已经摸索了净额和stackoverflow,却没有找到适合我的案例。这是我的信息来源:
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
}
我想要从终端执行命令。 cd路径; 。/配置; make
所以我需要按顺序运行每个命令,并等待最后一个命令完成后再继续。在当前版本的代码中,它当前表示 ./ configure:没有这样的文件或目录
我认为这是因为cd路径已执行并且在新的shell中执行。 ,而不是与上一个命令位于同一目录中。有任何想法吗?
更新我通过更改工作目录然后执行./configure和make命令来解决了该问题
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.
推荐答案
如果要在单个shell实例中运行多个命令,则需要调用该shell像这样:
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()
这将使Shell解释给定的命令。它还将允许您执行诸如 cd
之类的shell内置函数。请注意,以安全的方式将用户数据替换为这些命令并非易事。
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.
这篇关于在同一shell golang中运行多个Exec命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!