I want to redirect both stdout and stderr of a process to a single file. How do I do that in Bash?
相关问题
- How to get the return code of a shell script in lu
- Stop .htaccess redirect with query string
- JQ: Select when attribute value exists in a bash a
- UrlEncodeUnicode and browser navigation errors
- Invoking Mirth Connect CLI with Powershell script
相关文章
- 使用2台跳板机的情况下如何使用scp传文件
- How to get jQuery.ajax response status?
- In IntelliJ IDEA, how can I create a key binding t
- send redirect and setting cookie, using laravel 5
- Check if directory exists on remote machine with s
- shell中反引号 `` 赋值变量问题
- How get the time in milliseconds in FreeBSD?
- Reverse four length of letters with sed in unix
Now, simple echo will write to $LOG_FILE. Useful for daemonizing.
To the author of the original post,
It depends what you need to achieve. If you just need to redirect in/out of a command you call from your script, the answers are already given. Mine is about redirecting within current script which affects all commands/built-ins(includes forks) after the mentioned code snippet.
Another cool solution is about redirecting to both std-err/out AND to logger or log file at once which involves splitting "a stream" into two. This functionality is provided by 'tee' command which can write/append to several file descriptors(files, sockets, pipes, etc) at once: tee FILE1 FILE2 ... >(cmd1) >(cmd2) ...
So, from the beginning. Let's assume we have terminal connected to /dev/stdout(FD #1) and /dev/stderr(FD #2). In practice, it could be a pipe, socket or whatever.
The result of running a script having the above line and additionally this one:
...is as follows:
If you want to see clearer picture, add these 2 lines to the script:
"Easiest" way (bash4 only):
ls * 2>&- 1>&-
.For tcsh, I have to use the following command :
If use
command &> file
, it will give "Invalid null command" error.For situation, when "piping" is necessary you can use :
For example:
or
This bash-based solutions can pipe STDOUT and STDERR separately (from STDERR of "sort -c" or from STDERR to "sort -h").
Curiously, this works:
But this gives a syntax error:
You have to use:
I wanted a solution to have the output from stdout plus stderr written into a log file and stderr still on console. So I needed to duplicate the stderr output via tee.
This is the solution I found: