英文:
How to connect several pipe command in slice
问题
我查看了这个链接(https://stackoverflow.com/questions/10781516/how-to-pipe-several-commands),关于如何连接两个管道命令的代码如下:
func main() {
c1 := exec.Command("ls")
c2 := exec.Command("wc", "-l")
c2.Stdin, _ = c1.StdoutPipe()
c2.Stdout = os.Stdout
_ = c2.Start()
_ = c1.Run()
_ = c2.Wait()
}
但是现在我有多个命令,不止两个。命令将按照c1 | c2 | c3 | c4 | c5
的顺序运行,并且这些命令被放在一个切片中([](*exec.Cmd)
)。
问题:
我该如何循环遍历切片并按顺序执行命令,并获取最终结果?谢谢。
英文:
I have check this link about how to connect two pipe command as below code.
func main() {
c1 := exec.Command("ls")
c2 := exec.Command("wc", "-l")
c2.Stdin, _ = c1.StdoutPipe()
c2.Stdout = os.Stdout
_ = c2.Start()
_ = c1.Run()
_ = c2.Wait()
}
But now I have several command that is more than 2. the command will run as c1 | c2 | c3 | c4 | c5
, and these commands are put in a slice([](*exec.Cmd)
).
Question:
How can I loop slice and execute command as sequence and get the final result? Thanks.
答案1
得分: 2
你需要做的与你目前正在做的完全相同,只是你需要使用循环来完成。以下是一个完整的示例,除了你自己的示例之外,什么都不做:
package main
import (
"os"
"os/exec"
)
func main() {
var err error // 用于存储处理过程中的错误。你应该始终检查错误并处理它们。这不是可选项。
var commands []*exec.Cmd // 存储命令的切片。
// 你自己的命令
commands = append(commands, exec.Command("ls"))
commands = append(commands, exec.Command("wc", "-l"))
// 将每个命令的输入连接到前一个命令的输出(从第二个命令开始,显然)
for i := 1; i < len(commands); i++ {
commands[i].Stdin, err = commands[i-1].StdoutPipe()
if err != nil {
panic(err)
}
}
commands[len(commands)-1].Stdout = os.Stdout // 最后一个命令的输出连接到标准输出
// 启动每个命令。注意,反向顺序不是强制性的,你只需要避免现在运行第一个命令
for i := len(commands) - 1; i > 0; i-- {
err = commands[i].Start()
if err != nil {
panic(err)
}
}
// 运行第一个命令
commands[0].Run()
// 然后等待每个后续命令完成
for i := 1; i < len(commands); i++ {
err = commands[i].Wait()
if err != nil {
panic(err)
}
}
}
希望对你有所帮助!
英文:
You have to do exactly what you are doing currently, except that you have to do it with loops. Here is a full example which do nothing more than your own example case:
package main
import (
"os"
"os/exec"
)
func main() {
var err error // To store error during the processing. You should always check for errors and handle them. That's not an option.
var commands []*exec.Cmd // The slice that will store the commands.
// You own commands
commands = append(commands, exec.Command("ls"))
commands = append(commands, exec.Command("wc", "-l"))
// Connect each command input to the output of the previous command (starting with the second command, obviously)
for i := 1; i < len(commands); i++ {
commands[i].Stdin, err = commands[i - 1].StdoutPipe()
if err != nil {
panic(err)
}
}
commands[len(commands)-1].Stdout = os.Stdout // Last command output is connected to the standard output
// Start each command. note that the reverse order isn't mandatory, you just have to avoid runing the first command now
for i := len(commands) - 1; i > 0; i-- {
err = commands[i].Start()
if err != nil {
panic(err)
}
}
// Run the first command
commands[0].Run()
// Then wait for each subsequent command to finish
for i := 1; i < len(commands); i++ {
err = commands[i].Wait()
if err != nil {
panic(err)
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论