简体   繁体   English

如何重定向zsh / bash的stdout并稍后还原

[英]How to redirect stdout of zsh/bash and restore later

In python, you can 在python中,您可以

sys.stdout = open('log', 'w') # begin redirect

then output will write to log instead. 然后输出将改为写入log

You can restore back to normal behavior with 您可以使用以下方法恢复到正常行为

sys.stdout = sys.__stdout__   # end redirect, restore back

How to achieve similar effect in zsh & bash? 如何在zsh和bash中实现类似的效果?

PS, PS,

  • stdout of command in subshell should also be redirected. 子外壳程序中的命令stdout也应重定向。
  • ls > log is not what I want. ls > log不是我想要的。

To clarify, what I want is 为了澄清,我想要的是

ls   # output to terminal
# begin redirect to `log`
ls   # output to `log`
find -type f   # output to `log`
...  # output to `log`
# end redirect, restore back
ls   # output to terminal

Edit Below are not what I want 编辑下面不是我想要的

  • redirection a group of command. 重定向一组命令。
  • tail -f for monitoring. tail -f用于监视。

As The first few lines of this question stated, what I want is 正如此问题的前几行所述,我想要的是

# ...
cmd1    # normal behavior
# begin redirection
cmd2   # redirect to file
# some times later
cmd2   # redirect to file
# ...
cmdN   # redirect to file
# end redirection
cmdN+1 # normal behavior
# ...

You can use the tee command to log to a file as well as print to console: 您可以使用tee命令登录到文件以及打印到控制台:

ls                         # output to terminal
# begin redirect to `log`
ls  | tee -a log           # output to `log`
find -type f | tee -a log  # output to `log`
...  # output to `log`
# end redirect, restore back
ls   # output to terminal

Typically, you would redirect output for a command group, rather than redirecting and restoring output for the shell itself. 通常,您将重定向命令组的输出,而不是重定向和还原Shell本身的输出。

ls                    # to terminal
{
  ls
  find -type f
} > log               # to log
ls                    # to terminal again

Standard output for the command group delimited by { ... } as a whole is redirected to a file. 整体上由{ ... }分隔的命令组的标准输出将重定向到文件。 The commands in that group inherit their standard output from the group, not directly from the shell. 该组中的命令从该组继承其标准输出,而不是直接从Shell继承。

This is similar to doing the following in Python: 这类似于在Python中执行以下操作:

from contextlib import redirect_stdout

print("listing files to terminal")
with open("log", "w") as f, redirect_stdout(f):
    print("listing files to log")
    print("finding files")
print("listing files to terminal")

In shell, the equivalent of your forced redirection of standard output can be accomplished by using exec , as demonstrated by oguz ismail , though the command group makes it clearer where the redirection begins and ends. 在shell中,等效的标准输出强制重定向可以通过使用exec完成,如oguz ismail所示 ,尽管命令组可以使重定向的开始和结束位置更加清楚。 (It also avoids needing to find an unused file descriptor and remembering even more arcane shell syntax.) (这还避免了需要查找未使用的文件描述符并避免记住更多的奥秘shell语法。)

Permanent redirections are made using exec . 使用exec进行永久重定向。 For example: 例如:

ls              # to tty 
exec 3>&1 >log  # redirect to log, save tty
ls              # to log
find -type f    # ditto
exec >&3        # restore tty
ls              # to tty

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

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