【发布时间】:2019-11-25 15:37:46
【问题描述】:
在python中,你可以
sys.stdout = open('log', 'w') # begin redirect
然后输出将改为写入log。
您可以使用
恢复正常行为sys.stdout = sys.__stdout__ # end redirect, restore back
如何在zsh & bash中实现类似的效果?
附言,
- 子shell中命令的stdout也应该被重定向。
-
ls > log不是我想要的。
澄清一下,我想要的是
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
编辑 以下不是我想要的
- 重定向一组命令。
- tail -f 用于监控。
正如这个问题的前几行所说, 我想要的是
# ...
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
# ...
【问题讨论】:
标签: bash zsh io-redirection