2016-12-28 2 views
1

Goで3つ以上のコマンドをパイプするにはどうすればいいですか(たとえばls | grep | wc)?私は2つのコマンドをパイプするためのものです、このコードを変更しようとしましたが、正しい方法を把握することはできません。、のos.exec()で3つ以上のコマンドをパイプしますか?

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() 
} 
http://stackoverflow.com/a/10953142/3761308 
+0

可能性のある重複した[どのようにゴーで、パイプコマンドをいくつかに?](HTTP ://stackoverflow.com/questions/10781516/how-to-pipe-several-commands-in-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() 
} 
関連する問題