File Descriptors in Unix-like Systems
0
: Standard Input (stdin)1
: Standard Output (stdout)2
: Standard Error (stderr)
Redirection Operators
>
: Redirects stdout to a file.2>
: Redirects stderr to a file.&>
: Redirects both stdout and stderr to a file (in some shells).>&
: Redirects one file descriptor to another.
Explanation of 2>&1
2>&1
: This means "redirect stderr (file descriptor 2) to the same place as stdout (file descriptor 1)."- This is useful when you want both stdout and stderr to be combined and sent to the same destination, which in this case, is specified by the next part of the command.
Explanation of >log.txt
>log.txt
: This means "redirect stdout to a file namedlog.txt
."- Because of the
2>&1
earlier in the command, stderr is also redirected to stdout, and both are then redirected tolog.txt
.
Combined Effect
The combined command ./make.sh rk3568 2>&1 >log.txt
works as follows:
2>&1
: Redirects stderr (2) to stdout (1).>log.txt
: Redirects stdout (and now stderr as well) tolog.txt
.
Correct Command for Logging Both stdout and stderr
However, there’s a common misconception in the ordering of redirection. The way it is currently written may not work as intended in all shells. The command should typically be written as:
./make.sh rk3568 >log.txt 2>&1
Here’s why:
>log.txt
: Redirects stdout tolog.txt
.2>&1
: Redirects stderr to wherever stdout is currently going (which islog.txt
after the previous redirection).
Correct Usage in Practice
./make.sh rk3568 >log.txt 2>&1
This command ensures that both stdout and stderr are captured in the log.txt
file, allowing you to see all output (both normal messages and error messages) in one place.
Summary
2>&1
redirects stderr to stdout.>log.txt
redirects stdout tolog.txt
.- The correct sequence is
./make.sh rk3568 >log.txt 2>&1
to ensure both stdout and stderr are logged tolog.txt
.
This way, you will capture the complete output of the ./make.sh rk3568
command, including any error messages, in the log.txt
file.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。