Ignore error and get ls file count


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Ignore error and get ls file count
# 1  
Old 04-29-2009
Ignore error and get ls file count

I am trying to figure out how to run the below variable assignment in a shell script so that it will snuff the "no such file or directory" and just pass the count value. That way I can do a valid compare in the next step.

Code:
num=`ls /appsrv/tmp/PrjRefData_20090428-*_in.xml | wc -l`

ls: PrjRefData_20090428-*_in.xml: No such file or directory
0

I have tried various redirection but can't seem to figure it out. This obviously only happens when the ls returns no matches.

Any assistance is appreciated. Thanks
# 2  
Old 04-29-2009
use this

Code:
num=`ls /appsrv/tmp/PrjRefData_20090428-*_in.xml 2>/dev/null | wc -l`


cheers,
DevaraJ Takhellambam
# 3  
Old 04-29-2009
in my test, simply redirecting stderr of the ls command to /dev/null achived this:

num=`ls /appsrv/tmp/PrjRefData_20090428-*_in.xml 2>/dev/null | wc -l`

you could also add some code after the ls to see the return code is non-zero ($?), but it seems from testing the above that this is not nessesary as the above line set num to 0 for that example.

This is what you could add, as the very next line of execution, for reduntant error checkig, or an alternate solution:

if [ $? ne 0 ]
then
num=0
fi
# 4  
Old 04-29-2009
Thanks all! That solution will work perfect.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep and ignore list from file

cat /tmp/i.txt '(ORA-28001|ORA-00100|ORA-28001|ORA-20026|ORA-20025|ORA-02291|ORA-01458|ORA-01017|ORA-1017|ORA-28000|ORA-06512|ORA-06512|Domestic Phone|ENCRYPTION)' grep -ia 'ORA-\{5\}:' Rep* |grep -iavE `cat /tmp/i.txt` grep: Unmatched ( or \( Please tell me why am i getting that (6 Replies)
Discussion started by: jhonnyrip
6 Replies

2. Shell Programming and Scripting

Error files count while coping files from source to destination locaton as well count success full

hi All, Any one answer my requirement. I have source location src_dir="/home/oracle/arun/IRMS-CM" My Target location dest_dir="/home/oracle/arun/LiveLink/IRMS-CM/$dc/$pc/$ct" my source text files check with below example.text file content $fn "\t" $dc "\t" $pc "\t" ... (3 Replies)
Discussion started by: sravanreddy
3 Replies

3. Shell Programming and Scripting

How to ignore error in command in bash script?

Hello, i have bash script where im cycling some command for different lines in external file. example: while read domain;do nslookupout=$(nslookup -type=ns $domain) || true another commands done < filenamewithdomains i added: || true after the command in belief it will just skip... (6 Replies)
Discussion started by: postcd
6 Replies

4. Emergency UNIX and Linux Support

How to ignore Netout Error.?

Hi All , Am using unix KSH . I would like to clarify two doubts. Whenever am transfering the zero size file am getting the warning message netout. files will be transferred to target server or not? I have used the code as if then VAR="ftp.sh -c put ${FILE} ${FILE}... (2 Replies)
Discussion started by: Venkatesh1
2 Replies

5. Shell Programming and Scripting

Ignore garbage output file

Hi All, below is my shell script #!/bin/sh set -x echo "test for multiple values" UIDPSWD=`cat /projects/feeds/twest/uidpswd` echo "oracle connection test" full=/projects/feeds/twest/test_file values=`cut -d'|' -f1 $full|sed -e "s/.*/'&'/" -e 's/$/,/g' -e '$s/,$//'` sqlplus $UIDPSWD... (2 Replies)
Discussion started by: krupasindhu18
2 Replies

6. Shell Programming and Scripting

how to ignore multiline comment from a file while reading it

Hi friends , I want to ignore single and multiline comment( enclosed by " \* *\" ) of a file whle reading it. I am using the below code. nawk '/\/\*/{f=1} /\*\//{f=0;next} !f' proc.txt | while read str do ... done The problem is its working partially. that is its failing in one... (1 Reply)
Discussion started by: neelmani
1 Replies

7. UNIX for Dummies Questions & Answers

cat : ignore I/O error

Hello, I have to backup some cds but I get an I/O error message when I hit a bad sector (using the command cat) and my question is : what can I do to force the system to ignore theses errors (or all kind of errors) and let the process keep going? (sorry for any eventuals language errors, I am... (6 Replies)
Discussion started by: Actraiser47
6 Replies

8. Shell Programming and Scripting

Getting Sum, Count and Distinct Count of a file

Hi all this is a UNIX question. I have a large flat file with millions of records. col1|col2|col3 1|a|b 2|c|d 3|e|f 3|g|h footer**** I am supposed to calculate the sum of col1 1+2+3+3=9, count of col1 1,2,3,3=4, and distinct count of col1 1,2,3=c3 I would like it if you avoid... (4 Replies)
Discussion started by: singhabhijit
4 Replies

9. Shell Programming and Scripting

reading from a file and pass as variables and ignore # in the file

file.txt contains ------------------ sat1 1300 #sat2 2400 sat3 sat4 500 sat5 I need to write a shell script that will output like the below #output sat1.ksh 1300 sat3.ksh sat4.ksh 500 sat5.ksh my try ------- (4 Replies)
Discussion started by: konark
4 Replies

10. UNIX for Dummies Questions & Answers

How to count the record count in an EBCDIC file.

How do I get the record count in an EBCDIC file on a Linux Box. :confused: (1 Reply)
Discussion started by: oracle8
1 Replies
Login or Register to Ask a Question