The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 07-02-2009
scottn scottn is online now Forum Advisor  
VIP Member
  
 

Join Date: Jun 2009
Location: Zürich, CH
Posts: 1,047
The fist "ls" is used to test if any files matching this criteria are found. The output of ls is "thrown away" (to /dev/null) - the point being to know if there are such files, not to list them. If files are found, the "if [ $? -eq 0 ]" will be true (the ls returned 0 meaning success - files were found). But ls will write to standard error if no files are found. You can get rid of the error by directing standard error to /dev/null too. The simplest way is to direct standard error to standard output (which you threw away to /dev/null), so...

Change:

Code:
ls -1rt abc[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt > /dev/null
To:

Code:
ls -1rt abc[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].txt > /dev/null 2>&1
I hope this was explanatory and not at all boring to read!

Last edited by scottn; 07-02-2009 at 07:10 PM..