LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2004, 01:23 PM   #1
elyk
Member
 
Registered: Jun 2004
Distribution: Slackware
Posts: 241

Rep: Reputation: 49
1> /dev/null 2> /dev/null


I've seen chunks of code in lots of scripts that look something like this:
Code:
if ! ..... 1> /dev/null 2> /dev/null ; then
What does this do? It looks like some kind of comparison, but it dumps everything in /dev/null. I don't understand.

-elyk
 
Old 09-18-2004, 01:38 PM   #2
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
1> redirect stdout to /dev/null
2> redirect stdin to /dev/null


> is not acting as the greater than operator in this case but as a redirection operator.
for example

Code:
echo rubbish > temp.txt
will write the string "rubbish" to temp.txt

you might want to read the man page for bash
 
Old 09-18-2004, 01:45 PM   #3
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
just a small correction - 2> is for stderr, not stdin (stdin is 0).
 
Old 09-18-2004, 01:45 PM   #4
elyk
Member
 
Registered: Jun 2004
Distribution: Slackware
Posts: 241

Original Poster
Rep: Reputation: 49
Doesn't 2> redirect stderr? If so, this redirects stdout and stderr to /dev/null, so shouldn't this always give you nothing?
 
Old 09-18-2004, 01:53 PM   #5
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
oh my.... i think sleep is my only cure.

Quote:
Originally posted by CroMagnon
just a small correction - 2> is for stderr, not stdin (stdin is 0).
 
Old 09-18-2004, 02:01 PM   #6
CroMagnon
Member
 
Registered: Sep 2004
Location: New Zealand
Distribution: Debian
Posts: 900

Rep: Reputation: 33
Yes, adding this to the end of a command basically hides all output. This is usually done in scripts to prevent too much junk printing up on the console when it's not necessary.
 
Old 09-18-2004, 02:02 PM   #7
elyk
Member
 
Registered: Jun 2004
Distribution: Slackware
Posts: 241

Original Poster
Rep: Reputation: 49
yeah, you squeezed in your reply just before mine.
 
Old 09-18-2004, 02:12 PM   #8
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,760
Blog Entries: 4

Rep: Reputation: 78
Just to add some more info to this thread...

This sort of output redirection is typically used (but not limited to) by distro init scripts, so instead of seeing the output of all the startup commands you see the much more friendly:
Code:
* mounting filesystems                                        [  ok  ] 
* starting swap                                               [  ok  ]
etc...

Last edited by bulliver; 09-18-2004 at 02:13 PM.
 
Old 09-19-2004, 04:00 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Code:
 1> /dev/null 2> /dev/null
can be written more concisely
Code:
 >/dev/null 2>&1
 
Old 09-20-2004, 05:44 PM   #10
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Re: 1> /dev/null 2> /dev/null

Quote:
Originally posted by elyk
I've seen chunks of code in lots of scripts that look something like this:
Code:
if ! ..... 1> /dev/null 2> /dev/null ; then
What does this do? It looks like some kind of comparison, but it dumps everything in /dev/null. I don't understand.
Quote:
Doesn't 2> redirect stderr? If so, this redirects stdout and stderr to /dev/null, so shouldn't this always give you nothing?
Yes, all output from the command in the if-statement gets discarded this way. But the output is not what "if" uses to decide to execute some other commands or not. It is the exit-code from the program that matters for the if-statement. More precise: the exit-code of the last command inside the if-statement.

The exit-code is a number from 0 to 127 each program returns to its parent process (often the shell) when it ends. By convention exit-code 0 (zero) is returned when the program ran succesfully, and some other exit-code (non-zero) when some error ocurred, or when the program did not succeed in some other way.

You can see the exit-code of the last command executed in bash with "echo $?". Example:
Code:
ls /does_not_exist
echo $?

ls /proc/uptime
echo $?
As you see, apart from the output of ls it returns 1 when there was an error, so ls didn't succeed, and 1 when ls ran fine.

When you want a script to do something if a file or directory exists, you normally do:
Code:
if test -e /tmp/some_file ; then
    # ...
fi
Here, "test" is a program that checks if the file or directory exist, and return an exit-code of 0 or 1 depending on whether the file /tmp/some_file exists or not. Note that the "test" program doesn't do any output. It is the exit-code that matters to the "if" statement.

Also note that when you do this instead:
Code:
if [ -e /tmp/some_file ] ; then
    # ...
fi
You should think of "[" being a program that returns an exit-code ( "[" actually is a bash-builtin command, as is "test" by the way).

Another way to check whether a file (or directory) exists in a bash-script could be:
Code:
if ls /tmp/some_file >/dev/null 2>&1 ; then
    # ...
fi
The output of the "ls" command is directed to /dev/null to prevent it from appearing as output from the script. It's the exit-code what is needed by the "if" in the script.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What would happen if I where to cat /dev/mem > /dev/null Joey.Dale Linux - General 11 07-26-2009 12:46 PM
/dev/null linuxprogrammer Linux - Newbie 1 09-05-2005 07:07 AM
mv c:\WINDOWS /dev/null; mount /dev/hda treehead LinuxQuestions.org Member Intro 5 10-19-2004 08:53 AM
/dev/null jon_k Linux - Hardware 5 07-16-2003 02:40 PM
>/dev/null lackluster Linux - Networking 5 06-27-2002 09:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:22 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration