Operating System - HP-UX
1834200 Members
2908 Online
110066 Solutions
New Discussion

script to check for a file with csv extension

 
NDO
Super Advisor

script to check for a file with csv extension

Hi all! Please can I have some help. I need a script to check on a specific directory if there are files with CSV extension, if they do not exist the script should send an email notifying.
Thanks

F.R.
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: script to check for a file with csv extension

Hi:

Simply do something like this:

# cd /path
# ls *.csv > /dev/null 2>&1|| mailx -s "No CVS files present!" nandinho@some.net < /dev/null

Regards!

...JRF...
NDO
Super Advisor

Re: script to check for a file with csv extension

My idea was to have a script like this:

files=$(ls /tmp/*.csv > /dev/null | wc -l)

if [ "$files" != "0" ]

But its not working..

F.R.
James R. Ferguson
Acclaimed Contributor

Re: script to check for a file with csv extension

Hi (again):

> My idea was to have a script like this:
files=$(ls /tmp/*.csv > /dev/null | wc -l)

And this yields a value of ZERO when there *are* files since you throw away any STDOUT.

Look again at what I suggested. It leverages the return code of the 'ls' to ascertain whether or not there were files.

Regards!

...JRF...

rmueller58
Valued Contributor

Re: script to check for a file with csv extension

for file in `ls *csv`
do
if [ -f $file ]
then
command to process file
else
mail -s "File does not exist" name@domain.tld < /tmp/nofile.msg
fi
done
Dennis Handly
Acclaimed Contributor

Re: script to check for a file with csv extension

>rex m: for file in $(ls *csv); do
>if [ -f $file ]

There is no need for this check since the previous ls(1) would return nothing and skip the loop.
You may want to redirect stderr:
for file in $(ls *csv 2> /dev/null ); do