Question about Bash script for Linux
Question about Bash script for Linux
-------------------------------------------------------------------------------- 1) When running a script and it is piped to /dev/null &, what is it mean ? /usr/local/sbin/quotacheck1 > /dev/null & 2) When running a script and it is piped to /dev/null, what is it mean ? killall checkin2.sh > /dev/null 3) When running a script and it is piped to /dev/null 2>&1 &, what is it mean ? What is a 2 for ? What is a &1 for ? What is a & that goes after a 1 means ? /etc/checkin2.sh > /dev/null 2>&1 & Thank you for your help. |
/dev/null is like a black hole - everything you send there disappears.
>/dev/null means to redirect the output to go to /dev/null -- in other words not have coammnd out appear on the screen The ampersand at the end of a line means to execute the line in a background process. > /dev/null 2>&1 & means: send standard output to the bit bucket (/dev/null), plus whatever would come out on stderr (2 - standard error output) make that go wherever stdout ( 1 - standard output ) is going. & at the end of the line = run in background. the &> is called a dup (for duplicate): creating a duplicate file descriptor, in this case, whatever goes to 2 ends up in 1 0 - stdin 1 - stdout 2 - stderr |
1) The output just gets sent into oblivion. The ampersand sends the command into the background.
2) See above minus the backgrounding 3) The last ampersand backgrounds the task. 1=stdout (standard output stream) and 2=stderr (standard error stream). "2>&1" redirects messages from stderr to stdout. |
Redirecting (the > symbol) makes all standard output from the program go to the specified file. Redirecting to /dev/null means to send the output to oblivion. So basically the script doesn't want any of the output from quotacheck1, etc. to be shown.
The & at the end of a line tells the shell to run the program in the background. Otherwise you would have to wait for the program to finish before you got a prompt back again. The 2>&1 redirection means to send standard output and error output messages to the file (/dev/null = oblivion). |
Reply
Thank you jim mcnamara and david_ross for your help.
"2>&1" redirects messages from stderr to stdout. Is it true that the standard in is by default the screen which is the terminal where the user type it in on a keyboard ? Is it true that the standard out is by default the screen when 2>&1 is used? Is it true that the standard error is by default the screen when 2>&1 is used? |
Yes to all 3
|
unix was written in C.
By default C opens three file descriptors - 0, 1, 2 and they are called stdin, stdout, stderr. The same concept carries over to the command shell. A lot of C langauge conventions permeate unix. |
Thank you david_ross and jim mcnamara for your help.
|
Re: Reply
Quote:
|
reply
Thank you HKo for the additional answer.
|
All times are GMT -5. The time now is 02:55 PM. |