Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 将控制台的输出连续写入文件_Linux_Bash_Shell - Fatal编程技术网

Linux 将控制台的输出连续写入文件

Linux 将控制台的输出连续写入文件,linux,bash,shell,Linux,Bash,Shell,如何将控制台的输出连续写入一个文件,输出可能来自两个或多个程序。我的意思是,一旦我运行了所需的命令或代码,我就应该能够在该文件中查看该机器上运行的任何程序的所有标准输出。只需将标准输出重定向到该文件,如 command > file_name 只需将stdout重定向到该文件,如下所示 command > file_name 可以通过以下代码启动bash脚本: #!/bin/bash exec 2> /tmp/outfile.log # send stderr from

如何将控制台的输出连续写入一个文件,输出可能来自两个或多个程序。我的意思是,一旦我运行了所需的命令或代码,我就应该能够在该文件中查看该机器上运行的任何程序的所有标准输出。

只需将标准输出重定向到该文件,如

command > file_name

只需将
stdout
重定向到该文件,如下所示

command > file_name

可以通过以下代码启动bash脚本:

#!/bin/bash

exec 2> /tmp/outfile.log  # send stderr from your script to a log file
exec 1>&2                      # send stdout to the same log file
set -x
# the rest of your code ...

可以通过以下代码启动bash脚本:

#!/bin/bash

exec 2> /tmp/outfile.log  # send stderr from your script to a log file
exec 1>&2                      # send stdout to the same log file
set -x
# the rest of your code ...

如果你想查看输出并记录它,你可以使用
命令| tee>文件名
@l0b0,作为回答,输出重定向是一种方法,基本上重定向绕过标准输出,但是
tee
可以克服这个问题,并且应该是任何此类操作的理想选择如果你想查看输出并记录它,您可以使用
命令| tee>文件名
@l0b0,将此作为答案发布,输出重定向是一种方法,基本上重定向是绕过标准输出,但是
tee
可以克服,并且应该是任何此类操作的理想选择可能重复的抱歉,自我提升我自己接受的答案,但是链接问题是一个非常流行的问题,如果有什么需要补充的,应该放在那里。你能更准确地说出你想要什么吗@Rahul已经演示了重定向单个程序的输出是多么容易,但是如果你想重定向所有shell中的所有命令,那是一个完全不同的问题。很抱歉,我自己接受的答案的自我提升可能会重复,但是链接的问题是一个非常流行的问题,如果有什么需要补充的话,应该放在那里。你能更准确地说出你想要什么吗@Rahul已经演示了重定向单个程序的输出是多么容易,但是如果你想重定向所有shell中的所有命令,那是一个完全不同的问题。“set-x”做什么?set-x做什么?