【问题标题】:Go - Pipe 3 or more commands with os.exec()?Go - 使用 os.exec() 管道 3 个或更多命令?
【发布时间】:2017-05-12 17:30:46
【问题描述】:

如何在 Go 中将 3 个以上的命令连接在一起(例如 ls | grep | wc)?我试图修改这个用于管道 2 命令的代码,但无法找出正确的方法。,

package main

import (
    "os"
    "os/exec"
)

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()
}

https://stackoverflow.com/a/10953142/3761308

【问题讨论】:

标签: go


【解决方案1】:
package main

import (
    "os"
    "os/exec"
)

func main() {
    c1 := exec.Command("ls")
    c2 := exec.Command("grep", "-i", "o")
    c3 := exec.Command("wc", "-l")
    c2.Stdin, _ = c1.StdoutPipe()
    c3.Stdin, _ = c2.StdoutPipe()
    c3.Stdout = os.Stdout
    _ = c3.Start()
    _ = c2.Start()
    _ = c1.Run()
    _ = c2.Wait()
    _ = c3.Wait()
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-16
    • 2017-06-03
    • 2020-07-03
    • 1970-01-01
    • 2017-04-23
    • 1970-01-01
    相关资源
    最近更新 更多