這裏是我發現here解決方案(謝謝Malcolm)
func Readln(r *bufio.Reader) (string, error) {
var (
isPrefix = true
err error
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}
func handle(s string) {
//Do something with your string
}
func main() {
c := exec.Command("sh", "./tmuxpipe.sh")
err := c.Run()
if err != nil {
log.Fatal(err)
}
f, err := os.Open("/tmp/tmuxpipe")
if err != nil {
fmt.Printf("error opening file: %v\n", err)
os.Exit(1)
}
r := bufio.NewReader(f)
s, e := Readln(r)
for e == nil {
handle(s)
log.Println(s)
s, e = Readln(r)
}
}
這裏是tmuxpipe.sh:
mkfifo /tmp/tmuxpipe
tmux pipe-pane -o -t tmuxSession 'cat >> /tmp/tmuxpipe'
我之所以沒那裏只用exec.Command()
,是因爲某種原因超出我的理解:
c := exec.Command("tmux", "pipe-pane", "-o", "-t", "tmuxSession", 'cat >> /tmp/tmuxpipe'")
err := c.Run()
handleError(err)
沒有工作(對我而言)。 沒有發生錯誤,但tmux會話的輸出也沒有顯示。我希望這可以幫助任何人