关闭它们后重新打开STDOUT和STDERR吗? [英] Reopen STDOUT and STDERR after closing them?
问题描述
在bash中考虑我做了这个愚蠢的事情:
In bash consider I did this stupid thing:
$ exec 1>&-
$ exec 2>&-
我确定我还没有输掉这场比赛(哎呀,实际上我输了这场比赛),然后我可以重新连接到stdout.我以为我不知道怎么做.
I am sure I have not yet lost the game (oops actually I lost The Game) and I can reconnect to stdout. The think is I don't know how.
我尝试的第一件事是创建一个fifo,并使用另一个shell监视第一个shell的stdout.它不起作用,我不知道为什么:
First thing I tried is create a fifo and using another shell to monitor stdout of the first shell. It did not work, I don't know why:
$ mkfifo stdout
$ exec 1>stdout
$ echo "Hello stdout"
tty2
$ tail -f stdout
$ # nothing here
我到底如何重新连接关闭的
STDOUT
和STDERR
?
我知道一种解决方案是在使用STDOUT之前先保存它:
I know that a solution would be to save STDOUT before playing with it:
$ exec 3>&1
$ exec 1>&-
$ echo "Nothing will see this"
$ exec 1<&3 # Restoring stdout
推荐答案
我正在Ubuntu机器上运行此程序,因此不确定它是否对您有用,但这是我所做的:
I am running this on an Ubuntu machine, so I am not sure if it'll work for you, but this is what I did:
$ exec 1>&0
$ exec 2>&0
突然,我重新连接了STDOUT和STDERR.魔术!
Suddenly, I had STDOUT and STDERR reconnected. Magic!
说明::运行以下命令,我们将获得以下输出:
Explanation: Running the following commands, we get the following output:
$ ls -l /dev/stdout
lrwxrwxrwx 1 root root 15 Jun 11 23:39 /dev/stdout -> /proc/self/fd/1
$ ls -l /proc/self/fd/1
lrwx------ 1 jay jay 64 Jun 22 01:34 /proc/self/fd/1 -> /dev/pts/10
$ ls -l /proc/self/fd/
total 0
lrwx------ 1 jay jay 64 Jun 22 01:35 0 -> /dev/pts/10
lrwx------ 1 jay jay 64 Jun 22 01:35 1 -> /dev/pts/10
lrwx------ 1 jay jay 64 Jun 22 01:35 2 -> /dev/pts/10
lr-x------ 1 jay jay 64 Jun 22 01:35 3 -> /proc/12224/fd
由于所有三个fd都指向同一事物,因此我们只需将它们指向/dev/pts/10即可将它们恢复为正常状态,其中 exec 1>& 0
和 exec2>& 0
做
Since all three fd's point to the same thing, we can return them back to normal just by pointing to /dev/pts/10 which the exec 1>&0
and exec 2>&0
do
这篇关于关闭它们后重新打开STDOUT和STDERR吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!