我正在嘗試"hello world" | /usr/bin/pbcopy在 go 中執行 bash 命令:package mainimport ( "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`)}當我運行這個 go 文件時,它輸出:command is echo "hello world" | /usr/bin/pbcopyout"hello world" | /usr/bin/pbcopy我希望剪貼板等于“hello world”,但事實并非如此。更新我試過用 io.Pipepackage mainimport ( "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)}...但剪貼板仍然不等于“你好世界”
- 1 回答
- 0 關注
- 209 瀏覽
添加回答
舉報
0/150
提交
取消