1
我有這樣的代碼非常簡單:如何使用一個文件作爲標準輸入時EXEC命令
cmd := exec.Command("cat")
cmd.Stdout = os.Stdout
stdin, _ := cmd.StdinPipe()
file, _ := os.Open("file")
io.Copy(stdin, file)
_ = cmd.Run()
它的工作原理。但問題是,cmd.Run()
永遠不會返回。將文件寫入stdin後,似乎仍然等待更多內容。
我正在嘗試做什麼cat <file
確實會在讀取文件後退出cat
。
我想知道如何實現這一目標?
哈哈,是的,這只是爲了說明的目的。添加stdin.Close()的作品,謝謝! – hgl