dongpouda6700 2014-06-19 08:05 采纳率: 100%
浏览 21
已采纳

如何在切片中连接多个管道命令

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条回答 默认 最新

  • dtnqbre7980007 2014-06-19 08:22
    关注

    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)
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度