将多个命令的输出重定向到文件 [英] Redirect outputs of multiple commands to a file

查看:99
本文介绍了将多个命令的输出重定向到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我同时在Linux Shell中运行多个命令

I'm running multiple commands in my Linux shell at the same time, e.g.

echo "Line of text 1" && echo "Line of text 2" && complexthing | xargs printf "complexspecifier"

我想将所有输出重定向到file1.我知道我可以在每个单独的命令后添加>file1,但这似乎很庞大.我该怎么办?

I want to redirect all output to file1. I know I can add >file1 after each individual command but this seems bulky. How can I do this?

推荐答案

exec >file1   # redirect all output to file1
echo "Line of text1"
echo "Line of text2"
exec > /dev/tty  # direct output back to the terminal 

或者,如果您使用的计算机没有/dev/tty,则可以执行以下操作:

Or, if you are on a machine that doesn't have /dev/tty, you can do:

exec 5>&1 > file1  # copy current output and redirect output to file1 
echo foo
echo bar
exec 1>&5 5>&-  # restore original output and close the copy

这篇关于将多个命令的输出重定向到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆