Computing.Net > Forums > Unix > /dev/null

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

/dev/null

Reply to Message Icon

Name: Angie
Date: February 19, 2002 at 08:52:06 Pacific
Comment:

Can anyone tell me what actually takes place when I use the following command? This is in UNIX
grep "John Doe" file > /dev/null 2>&1



Sponsored Link
Ads by Google

Response Number 1
Name: James Boothe
Date: February 19, 2002 at 10:46:55 Pacific
Reply:

The grep command searches a file or fileset for the specified string(s), and will print each qualifying line.

In this case, you are asking it to search for and print lines that contain John Doe in the file named "file". But the normal output (stdout, file #1), will not go to your screen because it is being discarded (redirected to /dev/null). Also, any error output (stderr, file #2) is being merged with file #1, so it is also discarded.

So what use is this command if you discard its output? Execution of the command will set a status code that can be tested. Immediately after execution of that command, you can:

echo $?

This will be a zero for success (at least one qualifying line found), and a 1 if no qualifying lines are found, or any other error such as the file itself not found. Or optionally, the command can be put into an if statement like:

if grep ...

To demo output redirection, just do the following:

cd /tmp
touch goodfile
ls -l goodfile nofile >mystd 2>myerr

Above does a directory listing of a file that exists and one that does not, and redirects both stdout and stderr to disk files. Now look at the content of those two output files:

cat mystd
cat myerr

Now remove your test files with:

rm goodfile mystd myerr


0
Reply to Message Icon

Related Posts

See More


vmstat question FTP as Cron Job



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: /dev/null

adding printer port in Solaris www.computing.net/answers/unix/adding-printer-port-in-solaris/1505.html

Help with www.computing.net/answers/unix/help-with-/6436.html

convert string www.computing.net/answers/unix/convert-string/4230.html