Suppressing a message from being displayed

I have a script which checks for *.txt files in a particular directory and if no files were found then it goes into sleep for 10 secs and looks back for files again and if any files were found then the script does some processing with the files found, which is my requirement too.

FILE_EXISTS=`ls ${INPUT_DIR}/*.txt | wc -l`
if [ $FILE_EXISTS -eq 0 ];
then
echo "No file found hence sleeping for 10 secs..."
sleep 10;
continue;
fi 

but when ever no files were found a message saying

 ls: 0653-341 The file /home/sys/*.done does not exist. 

Is being displayed which I do not want to be displayed. So I used

 >/dev/null 2>&1 

in the script as below

FILE_EXISTS=`ls ${INPUT_DIR}/*.txt >/dev/null 2>&1 | wc -l`
if [ $FILE_EXISTS -eq 0 ];
      then
      echo "No file found hence sleeping for 10 secs..."
      sleep 10;
      continue;
fi 

now the message is not being displayed but even if files exists in the directory, the file count is not being assigned to the variable FILE_EXISTS. Doing so the script does not go further to do the required file processing but instead it still goes into sleep for 10 secs. Could any one please suggest any correction which needs to be made to my script.

If you're not glued to the 'ls' command, using the 'find' command instead would make things easier:

FILE_EXISTS=`find $INPUT_DIR -iname '*.txt' | wc -l`

The 'ls' command is designed to output that error regardless unless you suppress both stderr and stdout. The 'find' command is much more forgiving by outputting nothing if the files are not found.

Hope this helps.

How about this without any external commands.

TRY=${INPUT_DIR}/*.txt
if [ "$TRY" = "${INPUT_DIR}/\*.txt" ]
then
    echo No files there!
fi

Try this:

FILE_EXISTS=`ls ${INPUT_DIR}/*.txt 2>/dev/null | wc -l`
exists()
{ 
  [ -e "$1" ]
}

until exists "${INPUT_DIR}"/*.txt
do
  echo "No file found hence sleeping for 10 secs..."
  sleep 10
done

Works perfect for my requirement.
Thank you very much