Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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

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>myerrAbove 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 myerrNow remove your test files with:
rm goodfile mystd myerr

![]() |
vmstat question
|
FTP as Cron Job
|

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