简体   繁体   English

在使用`exec 1> file`之后,如何停止将STDOUT重定向到文件并恢复STDOUT的正常操作?

[英]After using `exec 1>file`, how can I stop this redirection of the STDOUT to file and restore the normal operation of STDOUT?

I am a newbie in shell scripting and I am using Ubuntu-11.10. 我是shell脚本的新手,我使用的是Ubuntu-11.10。 In the terminal after using exec 1>file command, whatever commands I give to terminal, its output doesn't get shown in terminal. 在使用exec 1>file命令后的终端中,无论我给终端发出什么命令,它的输出都不会显示在终端中。 I know that STDOUT is getting redirected to the file, the output of those commands gets redirected to file. 我知道STDOUT被重定向到文件,这些命令的输出被重定向到文件。

My questions are here 我的问题在这里

  1. Once I use exec 1>file , how can I get rid of this? 一旦我使用exec 1>file ,我怎么能摆脱这个? ie How can I stop the redirection of STDOUT to file and restore the normal operation of STDOUT (ie redirection to terminal rather than file)? 即如何停止STDOUT重定向到文件并恢复STDOUT的正常操作(即重定向到终端而不是文件)?

    I tried using exec 1>&- but it didn't work as this closes the STDOUT file descriptor. 我尝试使用exec 1>&-但它不起作用,因为这会关闭STDOUT文件描述符。

  2. Please throw light on this entire operation of exec 1>file and exec 1>&- 请注意exec 1>fileexec 1>&-整个操作。

  3. What will happen if we close the standard file descriptors 0, 1, 2 by using exec 0>&- exec 1>&- exec 2>&- ? 如果我们使用exec 0>&- exec 1>&- exec 2>&-关闭标准文件描述符0,1,2会发生什么?

Q1 Q1

You have to prepare for the recovery before you do the initial exec : 在执行初始exec操作之前,您必须为恢复做好准备:

exec 3>&1 1>file

To recover the original standard output later: 要在以后恢复原始标准输出:

exec 1>&3 3>&-

The first exec copies the original file descriptor 1 (standard output) to file descriptor 3, then redirects standard output to the named file. 第一个exec将原始文件描述符1(标准输出)复制到文件描述符3,然后将标准输出重定向到指定文件。 The second exec copies file descriptor 3 to standard output again, and then closes file descriptor 3. 第二个exec再次将文件描述符3复制到标准输出,然后关闭文件描述符3。

Q2 Q2

This is a bit open ended. 这有点开放。 It can be described at a C code level or at the shell command line level. 它可以在C代码级别或shell命令行级别进行描述。

exec 1>file

simply redirects the standard output (1) of the shell to the named file. 只需将shell的标准输出(1)重定向到指定的文件即可。 File descriptor one now references the named file; 文件描述符1现在引用命名文件; any output written to standard output will go to the file. 写入标准输出的任何输出都将转到该文件。 (Note that prompts in an interactive shell are written to standard error, not standard output.) (请注意,交互式shell中的提示会写入标准错误,而不是标准输出。)

exec 1>&-

simply closes the standard output of the shell. 只需关闭shell的标准输出。 Now there is no open file for standard output. 现在标准输出没有打开的文件。 Programs may get upset if they are run with no standard output. 如果程序在没有标准输出的情况下运行,程序可能会感到不安。

Q3 Q3

If you close all three of standard input, standard output and standard error, an interactive shell will exit as you close standard input (because it will get EOF when it reads the next command). 如果关闭所有三个标准输入,标准输出和标准错误,交互式shell将在关闭标准输入时退出(因为它在读取下一个命令时将获得EOF)。 A shell script will continue running, but programs that it runs may get upset because they're guaranteed 3 open file channels — standard input, standard output, standard error — and when your shell runs them, if there is no other I/O redirection, then they do not get the file channels they were promised and all hell may break loose (and the only way you'll know is that the exit status of the command will probably not be zero — success). shell脚本将继续运行,但它运行的程序可能会因为保证3个打开的文件通道 - 标准输入,标准输出,标准错误 - 以及当shell运行它们时,如果没有其他I / O重定向,然后他们没有获得他们承诺的文件渠道,所有地狱可能会破裂(你知道的唯一方法是命令的退出状态可能不会为零 - 成功)。

Q1 : There is a simple way to restore stdout to the terminal after it has been redirected to a file: Q1 :有一种简单的方法可以在将stdout重定向到文件后将其恢复到终端:

exec >/dev/tty

While saving the original stdout file descriptor is commonly required for it to be restored later, in this particular case, you want stdout to be /dev/tty so there is no need to do more. 虽然保存原始的stdout文件描述符通常需要稍后恢复,但在这种特殊情况下,您希望stdout/dev/tty因此不需要执行更多操作。

$ date
Mon Aug 25 10:06:46 CEST 2014
$ exec > /tmp/foo
$ date
$ exec > /dev/tty
$ date
Mon Aug 25 10:07:24 CEST 2014
$ ls -l /tmp/foo
-rw-r--r-- 1 jlliagre jlliagre 30 Aug 25 10:07 /tmp/foo
$ cat /tmp/foo
Mon Aug 25 10:07:05 CEST 2014

Q2 : exec 1>file is a slightly more verbose way of doing exec >file which, as already stated, redirect stdout to the given file, provided you have the right to create/write it. Q2exec 1>file是一种稍微冗长的执行exec >file方式, exec >file ,将stdout重定向到给定文件,只要你有权创建/写入它。 The file is created if it doesn't exist, it is truncated if it does. 如果文件不存在,则创建该文件,如果存在,则将其截断。

exec 1>&- is closing stdout, which is probably a bad idea in most situations. exec 1>&-正在关闭stdout,这在大多数情况下可能是一个坏主意。

Q3 : The commands should be Q3 :命令应该是

exec 0<&-
exec 1>&-
exec 2>&-

Note the reversed redirection for stdin. 注意stdin的反向重定向。

It might be simplified that way: 它可能会简化为:

exec <&- >&- 2>&-

This command closes all three standard file descriptors. 此命令将关闭所有三个标准文件描述符。 This is a very bad idea. 这是一个非常糟糕的主意。 Should you want a script to be disconnected from these channels, I would suggest this more robust approach: 如果您希望脚本与这些通道断开连接,我建议采用这种更强大的方法:

exec </dev/null >/dev/null 2>&1

In that case, all output will be discarded instead of triggering an error, and all input will return just nothing instead of failing. 在这种情况下,所有输出都将被丢弃而不是触发错误,并且所有输入都将返回任何内容而不是失败。

The accepted answer is too verbose for me. 接受的答案对我来说太冗长了。 So, I decided to sum up an answer to your original answer. 所以,我决定总结你原来答案的答案。

Using Bash version 4.3.39(2)-release 使用Bash版本4.3.39(2) - 发布

On a x86 32-Bit on Cygwin Machine 在Cygwin机器上的x86 32位上

GIVEN: 已知:

  • Stdin is fd #0. Stdin是fd#0。
  • Stdout is fd #1. Stdout是fd#1。
  • Stderr is fd #2. Stderr是fd#2。

ANSWER (written in bash): 答案(以bash编写):

exec 1> ./output.test.txt
echo -e "First Line: Hello World!"
printf "%s\n" "2nd Line: Hello Earth!" "3rd Line: Hello Solar System!"

# This is uneccessary, but
# it stops or closes stdout.
# exec 1>&-

# Send stdout back to stdin
exec 1>&0

# Oops... I need to append some more data.
# So, lets append to the file.
exec 1>> ./output.test.txt
echo -e "# Appended this line and the next line is empty.\n"

# Send stdout back to stdin
exec 1>&0

# Output the contents to stdout
cat ./output.test.txt

USEFUL-KEYWORDS: 有用的关键字:

There are also here-docs, here-strings, and process-substitution for IO redirection in Bourne, Bash, tcsh, zsh for Linux, BSD, AIX, HP, Busybox, Toybox and etcetera. 在Bourne,Bash,tcsh,zsh for Linux,BSD,AIX,HP,Busybox,Toybox等中还有here-docs,here-strings和IO重定向的进程替换。

While I completely agree with Jonathan's Q1, some systems have /dev/stdout , so you might be able to do exec 1>file; ...; exec 1>/dev/stdout 虽然我完全同意Jonathan的Q1,但有些系统有/dev/stdout ,所以你可以做exec 1>file; ...; exec 1>/dev/stdout exec 1>file; ...; exec 1>/dev/stdout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM