运行 同一个 shell golang 中的多个 Exec 命令
Run Multiple Exec Commands in the same shell golang
我无法弄清楚如何使用 os/exec 包 运行 多个命令。我浏览了网络和 Whosebug,但没有找到适合我的案例。这是我的来源:
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 path; ./configure; make
所以我需要按顺序 运行 每个命令并等待最后一个命令完成后再继续。在我当前的代码版本中,它目前表示 ./configure: no such file or directory
我认为这是因为 cd 路径执行并在新的 shell ./configure 中执行,而不是与上一个命令位于同一目录中。有任何想法吗?
更新 我通过更改工作目录然后执行 ./configure 和 make 命令解决了这个问题
err = os.Chdir(folderPath)
if err != nil {
fmt.Println("File Path Could not be changed")
os.Exit(1)
}
现在我仍然很想知道是否有办法在同一个 shell.
中执行命令
如果你想在一个 shell 实例中 运行 多个命令,你将需要用这样的东西调用 shell:
cmd := exec.Command("/bin/sh", "-c", "command1; command2; command3; ...")
err := cmd.Run()
这将得到 shell 来解释给定的命令。它还可以让您执行 shell 内置函数,例如 cd
。请注意,以安全的方式将用户数据替换为这些命令并非易事。
如果您只是想 运行 特定目录中的命令,则可以不使用 shell。您可以像这样设置当前工作目录来执行命令:
config := exec.Command("./configure", "--disable-yasm")
config.Dir = folderPath
build := exec.Command("make")
build.Dir = folderPath
...像以前一样继续。
我无法弄清楚如何使用 os/exec 包 运行 多个命令。我浏览了网络和 Whosebug,但没有找到适合我的案例。这是我的来源:
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 path; ./configure; make
所以我需要按顺序 运行 每个命令并等待最后一个命令完成后再继续。在我当前的代码版本中,它目前表示 ./configure: no such file or directory
我认为这是因为 cd 路径执行并在新的 shell ./configure 中执行,而不是与上一个命令位于同一目录中。有任何想法吗?
更新 我通过更改工作目录然后执行 ./configure 和 make 命令解决了这个问题
err = os.Chdir(folderPath)
if err != nil {
fmt.Println("File Path Could not be changed")
os.Exit(1)
}
现在我仍然很想知道是否有办法在同一个 shell.
中执行命令如果你想在一个 shell 实例中 运行 多个命令,你将需要用这样的东西调用 shell:
cmd := exec.Command("/bin/sh", "-c", "command1; command2; command3; ...")
err := cmd.Run()
这将得到 shell 来解释给定的命令。它还可以让您执行 shell 内置函数,例如 cd
。请注意,以安全的方式将用户数据替换为这些命令并非易事。
如果您只是想 运行 特定目录中的命令,则可以不使用 shell。您可以像这样设置当前工作目录来执行命令:
config := exec.Command("./configure", "--disable-yasm")
config.Dir = folderPath
build := exec.Command("make")
build.Dir = folderPath
...像以前一样继续。