亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

os.exec.Command 和 pbcopy

os.exec.Command 和 pbcopy

Go
米琪卡哇伊 2021-10-04 16:16:33
我正在嘗試"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 回答

  • 1 回答
  • 0 關注
  • 209 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號