这是我找到的解决方案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 会话的输出也没有显示。
希望对大家有帮助