Skip to content

Instantly share code, notes, and snippets.

@craftslab
Last active September 15, 2022 10:54
Show Gist options
  • Save craftslab/1fe9151fbf069a9e1341e4daebe43b5c to your computer and use it in GitHub Desktop.
Save craftslab/1fe9151fbf069a9e1341e4daebe43b5c to your computer and use it in GitHub Desktop.
go exec
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))
}
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()
}
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))
}
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())
}
}
// 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()
}
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
}
}
}
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