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

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

cmd.ExtraFiles 嘗試通過管道傳輸時失敗

cmd.ExtraFiles 嘗試通過管道傳輸時失敗

Go
互換的青春 2023-06-05 09:14:43
我試圖讓一根管子通向cmd.ExtraFiles我目前有錯誤說cannot use cmdstdout (type io.ReadCloser) as type []byte in argument to pipeR.Readcannot use cmdstdout (type io.ReadCloser) as type []byte in argument to fd3.Write這是我到目前為止的 gocodecmd2 = exec.Command("-i", "pipe:0", "-i", "pipe:1")cmd1 := exec.Command("command", "-o", "-")pipeR, pipeW, _ := os.Pipe()cmd2.ExtraFiles = []*os.File{    pipeW,}cmd1.Start()cmd1stdout, err := cmd1.StdoutPipe()if err != nil {    log.Printf("pipeThruError: %v\n", err)    return err}fd3 := os.NewFile(3, "/proc/self/fd/3")fd3.Write(cmd1stdout)pipeR.Read(cmd1stdout)pipeR.Close()pipeW.Close()fd3.Close()cmd3 = exec.Command("command", "-o", "-")stdin, stdinErr := cmd3.StdoutPipe()if stdinErr != nil {    log.Printf("pipeThruFStdinErr: %v\n", stdinErr)    return stdinErr}cmd3.Start()cmd2.Stdin = stdin編輯:添加了完整范圍目標是讓 cmd2 通過 cmd3 接受輸入Stdin,并讓 cmd1 輸出通過管道傳輸ExtraFiles
查看完整描述

1 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

這些類型在這里并不完全一致。具體來說,

cmd.StdoutPipe

返回一個io.ReadCloser

然而

pipeR.Read

期待[]byte作為輸入。

我相信您最終希望利用os 包的讀取和寫入功能來完成您的任務,如下所示:

package main


import (

? ? "log"

? ? "os"

? ? "os/exec"

)


func main() {

? ? cmd := exec.Command("command", "-o", "-")

? ? pipeR, pipeW, _ := os.Pipe()

? ? cmd.ExtraFiles = []*os.File{

? ? ? ? pipeW,

? ? }

? ? cmd.Start()

? ? cmdstdout, err := cmd.StdoutPipe()

? ? if err != nil {

? ? ? ? log.Printf("pipeThruError: %v\n", err)

? ? ? ? os.Exit(1)

? ? }


? ? buf := make([]byte, 100)

? ? cmdstdout.Read(buf)


? ? pipeR.Close()

? ? pipeW.Close()

? ? fd3 := os.NewFile(3, "/proc/self/fd/3")

? ? fd3.Write(buf)

? ? fd3.Close()

}


查看完整回答
反對 回復 2023-06-05
  • 1 回答
  • 0 關注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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