Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-12-2012
Registered User
 
Join Date: Jun 2012
Posts: 10
Thanks: 8
Thanked 0 Times in 0 Posts
/dev/null 2> &1


Code:
ls /oraarch/USUP/*.arc > /dev/null 2>&1
if [[ $? -eq 0 ]]
then
   /usr/bin/compress /oraarch/MOM/*.arc
   fi

did abit of understanding on /dev/null 2>&1

Quote:

You need to understand the theory first and then its upto you how and where you want to apply that theory. I'll try to explain above to you.

The greater-than (>) in commands like these redirect the program’s output somewhere. In this case, something is being redirected into /dev/null, and something is being redirected into &1.

Standard in, out and error:

There are three standard sources of input and output for a program. Standard input usually comes from the keyboard if it’s an interactive program, or from another program if it’s processing the other program’s output. The program usually prints to standard output, and sometimes prints to standard error. These three file descriptors (you can think of them as “data pipes”) are often called STDIN, STDOUT, and STDERR.

Sometimes they’re not named, they’re numbered! The built-in numberings for them are 0, 1, and 2, in that order. By default, if you don’t name or number one explicitly, you’re talking about STDOUT.

That means file descriptor 0 or fd0 denotes STDIN or standard input and file descriptor 1 or fd1 denotes STDOUT or standard output and file descriptor 2 or fd2 denotes STDERR or standard error.

You can see the command above is redirecting standard output into /dev/null, which is a place you can dump anything you don’t want (often called the bit-bucket), then redirecting standard error into standard output (you have to put an & in front of the destination when you do this).

The short explanation, therefore, is “all output from this command should be shoved into a black hole.” That’s one good way to make a program be really quiet!

Regards,
Tayyab
base on Tayyab explanation, am i right to say that the code above means to dump all the archived log into /dev/null and this is the part i cannot figure out, something is redirect into &1. What is the 2 here?

And what is if [[ $? -eq 0 ]]

And /usr/bin/compress /oraarch/MOM/*.arc

i cannot re- connect from this pieces of code cos i am trying to understand how it was being hook up all together.

Moderator's Comments:
edit by bakunin: use [QUOTE]..[/QUOTE] instead of {QUOTE}..{QUOTE}. The forum software understands the first, but not the latter. I have changed that for you.

Last edited by bakunin; 06-12-2012 at 05:56 AM..
Sponsored Links
    #2  
Old 06-12-2012
Registered User
 
Join Date: Aug 2006
Posts: 313
Thanks: 1
Thanked 47 Times in 45 Posts
The code does not dump any files into /dev/null. Lets have a look at it step by step:
1) ls /oraarch/USUP/*.arc lists all files in /oraarch/USUP with extension .arc to STDOUT
2) > /dev/null sends all what would go to STDOUT to nirvana, so you will not see any normal output of the ls-command
3) 2>&1 says STDERR should go to the same destination as STDOUT, so you will not see any errormessages from the ls-command
4) if [[ $? -eq 0 ]] $? holds the return code of the last command in your case the ls whose complete output was discarded. A return code of 0 means success. [[ ... ]] is the syntax for a condition and -eq inside such a construct means equal. Together this says if ls found any files do whats between then and fi .
5) /usr/bin/compress /oraarch/MOM/*.arc compress is used to ... compress files.
The Following User Says Thank You to cero For This Useful Post:
redologger (06-12-2012)
Sponsored Links
    #3  
Old 06-12-2012
Registered User
 
Join Date: Jun 2012
Posts: 10
Thanks: 8
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by cero View Post
The code does not dump any files into /dev/null. Lets have a look at it step by step:
1) ls /oraarch/USUP/*.arc lists all files in /oraarch/USUP with extension .arc to STDOUT
2) > /dev/null sends all what would go to STDOUT to nirvana, so you will not see any normal output of the ls-command
3) 2>&1 says STDERR should go to the same destination as STDOUT, so you will not see any errormessages from the ls-command
4) if [[ $? -eq 0 ]] $? holds the return code of the last command in your case the ls whose complete output was discarded. A return code of 0 means success. [[ ... ]] is the syntax for a condition and -eq inside such a construct means equal. Together this says if ls found any files do whats between then and fi .
5) /usr/bin/compress /oraarch/MOM/*.arc compress is used to ... compress files.
i have to say is clearer to me. But is there any link i can see some example like stdout, stderror.
    #4  
Old 06-12-2012
Ygor's Avatar
Ygor Ygor is offline Forum Staff  
Moderator
 
Join Date: Oct 2003
Location: 54.23, -4.53
Posts: 1,792
Thanks: 1
Thanked 101 Times in 91 Posts
I/O Redirection
The Following User Says Thank You to Ygor For This Useful Post:
redologger (06-12-2012)
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Redirecting standard out to /dev/null goes to file "/dev/null" instead jbmorrisonjr Shell Programming and Scripting 5 06-06-2012 07:27 PM
/dev/null 2>&1 Versus /dev/null 2>1 glev2005 UNIX for Dummies Questions & Answers 3 03-08-2011 01:20 PM
Insert string 'NULL' where there is a null value zilch Shell Programming and Scripting 8 05-27-2010 09:17 AM
/dev/null what is the use of it? webmunkey23 Shell Programming and Scripting 3 07-03-2008 07:47 AM
compare null with non-null nitin Shell Programming and Scripting 8 11-04-2006 06:58 PM



All times are GMT -4. The time now is 09:37 PM.