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 named log.txt."
  • Because of the 2>&1 earlier in the command, stderr is also redirected to stdout, and both are then redirected to log.txt.

Combined Effect

The combined command ./make.sh rk3568 2>&1 >log.txt works as follows:

  1. 2>&1: Redirects stderr (2) to stdout (1).
  2. >log.txt: Redirects stdout (and now stderr as well) to log.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:

  1. >log.txt: Redirects stdout to log.txt.
  2. 2>&1: Redirects stderr to wherever stdout is currently going (which is log.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 to log.txt.
  • The correct sequence is ./make.sh rk3568 >log.txt 2>&1 to ensure both stdout and stderr are logged to log.txt.

This way, you will capture the complete output of the ./make.sh rk3568 command, including any error messages, in the log.txt file.


Ackerman
1 声望0 粉丝

引用和评论

0 条评论