go run pipe.go
Last active
September 15, 2022 10:54
-
-
Save craftslab/1fe9151fbf069a9e1341e4daebe43b5c to your computer and use it in GitHub Desktop.
go exec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os/exec" | |
) | |
func main() { | |
cmd := exec.Command("/bin/bash", "-c", "ls /") | |
res, err := cmd.CombinedOutput() | |
if err != nil { | |
fmt.Println(string(res)) | |
panic(err) | |
} | |
fmt.Println(string(res)) | |
cmd = exec.Command("/bin/bash", "-c", "ls /notfile") | |
res, err = cmd.CombinedOutput() | |
if err != nil { | |
fmt.Println(string(res)) | |
panic(err) | |
} | |
fmt.Println(string(res)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os/exec" | |
) | |
func main() { | |
cmd := exec.Command("ls", "-l") | |
reader, _ := cmd.StdoutPipe() | |
cmd.Stderr = cmd.Stdout | |
go func() { | |
scanner := bufio.NewScanner(reader) | |
for scanner.Scan() { | |
msg := scanner.Text() | |
fmt.Printf("%s\n", msg) | |
} | |
}() | |
_ = cmd.Run() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"os/exec" | |
) | |
func main() { | |
outr, outw, _ := os.Pipe() | |
defer func() { _ = outr.Close() }() | |
defer func() { _ = outw.Close() }() | |
inr, inw, _ := os.Pipe() | |
defer func() { _ = inr.Close() }() | |
defer func() { _ = inw.Close() }() | |
cmd, _ := exec.LookPath("ls") | |
_, _ = os.StartProcess(cmd, []string{""}, &os.ProcAttr{ | |
Env: os.Environ(), | |
Files: []*os.File{inr, outw, outw}, | |
}) | |
_, _ = inw.Write([]byte("-l")) | |
buf := make([]byte, 256) | |
_, _ = outr.Read(buf) | |
fmt.Printf("%v\n", string(buf)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"fmt" | |
"os" | |
"os/exec" | |
) | |
func main() { | |
outr, outw, _ := os.Pipe() | |
defer func() { _ = outr.Close() }() | |
defer func() { _ = outw.Close() }() | |
inr, inw, _ := os.Pipe() | |
defer func() { _ = inr.Close() }() | |
defer func() { _ = inw.Close() }() | |
cmd, _ := exec.LookPath("ls") | |
_, _ = os.StartProcess(cmd, []string{""}, &os.ProcAttr{ | |
Env: os.Environ(), | |
Files: []*os.File{inr, outw, outw}, | |
}) | |
_, _ = inw.Write([]byte("-l")) | |
scanner := bufio.NewScanner(outr) | |
for scanner.Scan() { | |
fmt.Printf("%v\n", scanner.Text()) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Refer: https://bitcoden.com/answers/how-to-pipe-several-commands-in-go | |
package main | |
import ( | |
"io" | |
"os" | |
"os/exec" | |
) | |
func main() { | |
c1 := exec.Command("ls") | |
c2 := exec.Command("wc", "-l") | |
pr, pw := io.Pipe() | |
c1.Stdout = pw | |
c2.Stdin = pr | |
c2.Stdout = os.Stdout | |
c1.Start() | |
c2.Start() | |
go func() { | |
defer pw.Close() | |
c1.Wait() | |
}() | |
c2.Wait() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bufio" | |
"context" | |
"fmt" | |
"os/exec" | |
"time" | |
) | |
func main() { | |
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | |
defer cancel() | |
cmd := exec.Command("echo", "task") | |
// stderr, err := cmd.StderrPipe() | |
stdout, _ := cmd.StdoutPipe() | |
_ = cmd.Start() | |
scanner := bufio.NewScanner(stdout) | |
lines := make(chan string) | |
go func(scanner *bufio.Scanner) { | |
for scanner.Scan() { | |
lines <- scanner.Text() | |
} | |
}(scanner) | |
go func() { | |
_ = cmd.Wait() | |
cancel() | |
}() | |
L: | |
for { | |
select { | |
case line := <-lines: | |
fmt.Printf("%v\n", line) | |
case <-ctx.Done(): | |
break L | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os" | |
"os/exec" | |
) | |
func main() { | |
cmd, _ := exec.LookPath("ls") | |
_, _ = os.StartProcess(cmd, []string{""}, &os.ProcAttr{ | |
Env: os.Environ(), | |
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment