我无法弄清楚如何使用 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.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)
}
我想像从终端一样执行命令。 cd path; ./configure; make 所以我需要按顺序运行每个命令并在继续之前等待最后一个命令完成。使用我当前版本的代码,它目前说./configure: no such file or directory我认为这是因为 cd path 执行并在新的 shell 中执行 ./configure,而不是在上一个命令的同一目录中。有任何想法吗? 更新我通过更改工作目录然后执行 ./configure 和 make 命令解决了这个问题
err = os.Chdir(folderPath)
if err != nil {
fmt.Println("File Path Could not be changed")
os.Exit(1)
}
现在我仍然很想知道是否有办法在同一个 shell 中执行命令。
红颜莎娜
相关分类