Suppressing a message from being displayed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Suppressing a message from being displayed
# 1  
Old 11-12-2010
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.
Code:
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
Code:
 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
Code:
 >/dev/null 2>&1

in the script as below
Code:
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.
# 2  
Old 11-12-2010
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.
# 3  
Old 11-12-2010
How about this without any external commands.
Code:
TRY=${INPUT_DIR}/*.txt
if [ "$TRY" = "${INPUT_DIR}/\*.txt" ]
then
    echo No files there!
fi


Last edited by Chubler_XL; 11-12-2010 at 08:19 PM.. Reason: Fix typos
# 4  
Old 11-12-2010
Try this:
HTML Code:
FILE_EXISTS=`ls ${INPUT_DIR}/*.txt 2>/dev/null | wc -l`
# 5  
Old 11-12-2010
Code:
exists()
{ 
  [ -e "$1" ]
}

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


Last edited by Scrutinizer; 11-12-2010 at 10:39 PM..
# 6  
Old 11-15-2010
Quote:
Originally Posted by edidataguy
Try this:
HTML Code:
FILE_EXISTS=`ls ${INPUT_DIR}/*.txt 2>/dev/null | wc -l`
Works perfect for my requirement.
Thank you very much
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Colorful message is displayed in the terminal but not in the log file

Is there any way to print the colorful message in the terminal (which is the output of a simulation) to the log file with the same color. The colorful message at the terminal is obtained by the following codes: /////////////codes start here/////////////////////////////////////// ... (5 Replies)
Discussion started by: babunp114525
5 Replies

2. HP-UX

Suppressing errors from command output

Hi, When I run the command "print_manifest | grep "Main Memory", I get the note : # /opt/ignite/bin/print_manifest | grep "Main Memory" NOTE: Could not read the /etc/resolv.conf file. Main Memory: 196498 MB # How do I suppress the part : NOTE: Could not read the... (4 Replies)
Discussion started by: anaigini45
4 Replies

3. Filesystems, Disks and Memory

DISK ARRAY PROTECTION SUSPENDED message displayed following disk replacement

Hello, On 4/20/2018, we performed a disk replacement on our IBM 8202 P7 server. After the disk was rebuilt, the SAS Disk Array sissas0 showed a status of degraded. However, the pdisks in the array all show a status of active. We did see a message in errpt. DISK ARRAY PROTECTION SUSPENDED. ... (1 Reply)
Discussion started by: terrya
1 Replies

4. Shell Programming and Scripting

Suppressing the terminated message from pkill & killall commands in a bash script

Hi all, I've been pulling my hair out with this problem for 3 days:wall: now without success any help would be massively appreciated. Basically the script is used to shutdown a rails server so a new IP address can be assigned, the shutdown part is taken care of in function_one using the... (2 Replies)
Discussion started by: danmc
2 Replies

5. Red Hat

Suppressing Error Message is not working

I need to suppress the error message for commands. I have given the command like below lsb_release -dr 2>/dev/null But it is not working. Am testing with Red Hat Linux release 9. Guide me. Thanks in advance (2 Replies)
Discussion started by: forumguest
2 Replies

6. Shell Programming and Scripting

Suppressing space replacing by comma

hi i want to replace spaces by comma my file is ADD 16428 170 160 3 WNPG 204 941 No 204802 ADD 16428 170 160 3 WNPG 204 941 No 204803 ADD 16428 170 160 3 WNPG 204 941 No 204804 ADD... (9 Replies)
Discussion started by: raghavendra.cse
9 Replies

7. UNIX for Dummies Questions & Answers

suppressing an error message using the "ls" command

hi I'm new here so my question maybe be retarded or out of place: Is there a way to suppress the "No such file or directory" error message when using the "ls" command ? thanks (5 Replies)
Discussion started by: basher400
5 Replies

8. UNIX for Dummies Questions & Answers

Suppressing output to the screen

I want to check whether the variable read from the console is number or character , so i used echo $option|grep and checked the reuslt returned by grep cmd using $? But since I use echo the value is getting printed in the screen , I want to supress the o/p. Can anyone suggest how this can be... (1 Reply)
Discussion started by: rolex.mp
1 Replies

9. Shell Programming and Scripting

Suppressing one line in a file

Hello there, I am having a text file, the following is written in that text file: Full range of accounts: Account1 Account2 Account3 Account4 Now my question is. how to take that file and produce another file having only the accounts (i.e., suppressing the fist line of that... (4 Replies)
Discussion started by: charbel
4 Replies

10. Shell Programming and Scripting

suppressing keyboard input

Setup Info: This User Id and Password mention below are being used with the ISQL command to connect to a sybase database so they are likely to not be the same as those that were signed on from the session. Situation: Using a korn shell, the shell prompts for a User Id and Password. During the... (1 Reply)
Discussion started by: anthreedhr
1 Replies
Login or Register to Ask a Question