Something that nobody explains...Logging with the 0, 1, 2, > and & in *nix environments
Ever wondered what those greater than, less than and ampersand meant in commands that you encounter time to time? Well here is the answer:
I use three different input and output channels(STDIN, STDOUT and STDERR). A little bit of background on these: these are three pre connected input and output channels between a computer program and its environment, eg. a text terminal when it begins execution. These are called standard input(stdin), standard output(stdout) and standard error(stderr).
I use three different input and output channels(STDIN, STDOUT and STDERR). A little bit of background on these: these are three pre connected input and output channels between a computer program and its environment, eg. a text terminal when it begins execution. These are called standard input(stdin), standard output(stdout) and standard error(stderr).
File Descriptor(search this on google) for STDIN, STDOUT and STDERR are 0, 1 and 2 respectively.
Now when you look at the example below, you will feel like you have gained something(I certainly hope so with the condition that you didn’t know about this before, or had only partial clue).
Examples for output redirection:
Example 1:
------------------------------ -----------------
someprogram.bat 2>&1
------------------------------ -----------------
Example 1:
------------------------------
someprogram.bat 2>&1
------------------------------
Explanation:
someprogram.bat : is name of program or command
2: stderr (remember 2 is file descriptor for STDERR)
> : symbol for redirection or for appending or sending something to whatever comes next
&: in such scenarios & is used just before the name of the target(maybe for dereferencing?)
1: stdout (recall? 1 is file descriptor for STDOUT)
summary of explanation: errors from program goes to 2(by default) but then whatever is in 2, we are manually redirecting to 1.
someprogram.bat : is name of program or command
2: stderr (remember 2 is file descriptor for STDERR)
> : symbol for redirection or for appending or sending something to whatever comes next
&: in such scenarios & is used just before the name of the target(maybe for dereferencing?)
1: stdout (recall? 1 is file descriptor for STDOUT)
summary of explanation: errors from program goes to 2(by default) but then whatever is in 2, we are manually redirecting to 1.
That’s all there is to it.
Example 2:
------------------------------ -----------------
someprogram.bat 2>&1 | tee prog_log_error.log
------------------------------ -----------------
someprogram.bat 2>&1 | tee prog_log_error.log
------------------------------
Explanation:
read the above explanation then continue to next line here.
| : is piping, just like piping water from one tube to another.
tee: name of an internal *nix command. (this command sends copies of output to two different
destinations: a file and a console(the screen?)
prog_log_error.log: name of a log file that we give(make sure you give the full path of this file) where tee program sends whatever is in 1(stdout). Tee also sends the same content(in 1) to console(stdout).
read the above explanation then continue to next line here.
| : is piping, just like piping water from one tube to another.
tee: name of an internal *nix command. (this command sends copies of output to two different
destinations: a file and a console(the screen?)
prog_log_error.log: name of a log file that we give(make sure you give the full path of this file) where tee program sends whatever is in 1(stdout). Tee also sends the same content(in 1) to console(stdout).
Just one more fact: most *nix shells allow both standard output and standard error to be redirected to the same file using:
------------------------------ -----------------
&> your_log_file_name
------------------------------ -----------------
------------------------------
&> your_log_file_name
------------------------------
Skol!
~Nirmal
“First they ignore you. Then they laugh at you. Then they fight you. Then you win.” – Mahatma Gandhi
0 comments:
Post a Comment