IO redirect is fundamental in Linux shell -- one basic thing you should pay your attention when learning/using Linux.
3 Basic File descriptors:
File Descriptor | Name | Description |
0 | stdin | standard input |
1 | stdout | standard output |
2 | stderr | standard error |
Below I'll introduce 3 advanced usage, well they are also very common scenarios.
1. Redirect stderr to stdout
Note the "2>&1" sequence, please keep them there. This command will redirect stderr to stdout.
2. Output stdout and stderr to a file simultaneously
Think about this: you are compiling a huge project which uses Makefile, the compilation failed and a lot of error message shown on the scree. It could be easier for you to analyze these error messages in your favorite editor.
Here is the solution. Either of below ways will achieve this. And of course the show-up sequence is exactly the same as on the screen.
$ find /var -name run &>file |
or
$ find /var -name run >file 2>&1 |
Please note:(1) The sequence of "&" and ">" on two ways is different, don't mix them up!
(2) For the second one, you must assign stdout first, then stderr.
3. Ignore stdout or stderr
Sometimes you don't want to see some message just because they are trifling or you want to keep a clean screen. You can put everything into the bottomless pit: /dev/null:
This example ignores stderr:
$ find /var -name run 2>/dev/null |
This example ignores both stdout and stderr:
$ secret-app 1>/dev/null 2>/dev/null |
No comments:
Post a Comment