开发者

Can stdout, stderr always tee to a regular file?

开发者 https://www.devze.com 2023-01-09 14:16 出处:网络
Can I configure bash on Linux to write a copy of all stdout and stderr to a regular file, without having to specify 开发者_开发百科the redirection for each command?

Can I configure bash on Linux to write a copy of all stdout and stderr to a regular file, without having to specify 开发者_开发百科the redirection for each command?

Thanks, Kent


You can use script to create a log of your shell session


If you are writing a script, I'd recommend write a shell function and redirect its output. You won't have to redirect each command of that function then. I hardly see any meaning in redirections in interactive shell anyway.


The exec command, if not given a command to execute, will apply its redirections to the current shell. In my testing, however, this does not seem to work that well for pipes. I'd suggest using a subshell, which you can redirect normally.

(
   echo hello
   echo hello err >&2
) 2>&1 | tee logfile.txt

If you only need a transcript, not a continuous tee, exec >& logfile might work.

Also, a traditional trick is to use a remote login tool (ssh or telnet) piped to tee to create a log of an interactive session. screen also supports logging.

0

精彩评论

暂无评论...
验证码 换一张
取 消