Control the output of the command redirection
文章目录
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>&1earlier 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>&1Here’s why:
>log.txt: Redirects stdout tolog.txt.2>&1: Redirects stderr to wherever stdout is currently going (which islog.txtafter the previous redirection).
Correct Usage in Practice
./make.sh rk3568 >log.txt 2>&1This 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>&1redirects stderr to stdout.>log.txtredirects stdout tolog.txt.- The correct sequence is
./make.sh rk3568 >log.txt 2>&1to 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.

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论