将新xterm的输出重定向回原始终端

问题描述 投票:1回答:2

例如,我有一个非常简单的脚本,ping.sh

#!/bin/bash
/usr/bin/xterm -e ping localhost

现在,ping的输出只出现在新的xterm中。我希望输出显示在原始终端(ping.sh的stdout)以及新的xterm中。有没有办法做到这一点?

PS:我正在争夺这个头衔。

linux bash xterm
2个回答
0
投票

看起来像一个奇怪的事情,但这可能有效:

#!/bin/bash
f=$(mktemp)
touch "$f"
tail -f "$f" &
/usr/bin/xterm -e "sh -c 'ping localhost 2>&1 | tee -a $f'"

0
投票

或者,可以使用命令tty获取连接到标准输入的终端的文件名,然后在新终端中使用tee将输出复制到旧终端。

/usr/bin/xterm -e "ping localhost | tee $(tty)"

当然,这仅在未使用重定向标准输入调用脚本时才有效。

如果使用重定向的stdin调用脚本,则可以使用shell - How to get the real name of the controlling terminal? - Unix & Linux Stack Exchange中的解决方案。 readlink /proc/self/fd/1,或ps(需要一些输出解析)

© www.soinside.com 2019 - 2024. All rights reserved.