I am trying to execute bash command "hello world" | /usr/bin/pbcopy
inside go:
package main
import (
"fmt"
"os/exec"
"strings"
)
func Cmd(cmd string) {
fmt.Println("command is ", cmd)
parts := strings.Fields(cmd)
head := parts[0]
parts = parts[1:len(parts)]
out, err := exec.Command(head, parts...).Output()
if err != nil {
fmt.Printf("%s", err)
}
fmt.Println("out")
fmt.Println(string(out))
}
func main() {
Cmd(`echo "hello world" | /usr/bin/pbcopy`)
}
When I run this go file, it outputs:
command is echo "hello world" | /usr/bin/pbcopy
out
"hello world" | /usr/bin/pbcopy
I expect clipboard to be equal to "hello world" but it is not.
Update
I've tried to use io.Pipe
package main
import (
"bytes"
"io"
"os"
"os/exec"
)
func main() {
c1 := exec.Command(`echo "hello world"`)
c2 := exec.Command("/usr/bin/pbcopy")
r, w := io.Pipe()
c1.Stdout = w
c2.Stdin = r
var b2 bytes.Buffer
c2.Stdout = &b2
c1.Start()
c2.Start()
c1.Wait()
w.Close()
c2.Wait()
io.Copy(os.Stdout, &b2)
}
... but clipboard is still not equal to "hello world"