Record count checking for multiple files through for-loop

Hi Friends,

I wrote one shell script to check the record count in two files and that will send us the notification activity if found zero record count.

What i did is I created for loop and checking the count for both of the files but what is happening is for first file has data then it's getting exit and not checking another file count.

I have prepared script as below please check it and correct me if anything wrong.

#!/bin/sh
a=/tjx/iisNAS/data/edw/fp/output/common/sample.txt
b=/tjx/iisNAS/data/edw/fp/output/common/sample1.txt
for file in `wc -l $a |awk -F " " '{print $1}'` `wc -l $b |awk -F " " '{print $1}'`
do
if [ $file -eq 0 ];
then
mailx -s " $file Data is not available in lookup" 
exit 1
else
mailx -s " $file /tjx/iisNAS/data/edw/fp/output/common/sample.txt Data is available in lookup" abc@yahoo.com
exit 0
fi

Thanks in advance.

Not sure I grasp your logics. Why dont you try like this (untested):

a=/tjx/iisNAS/data/edw/fp/output/common/sample.txt
b=/tjx/iisNAS/data/edw/fp/output/common/sample1.txt
for file in $a $b
do if [ 0 -eq $(wc -l $file) ]
   then mailx ...
        exit 1
   else mailx ...
        exit 0
   fi
done

Thanks a lot friend...
I will try now..

Better use

[ 0 -eq $(wc -l <$file) ]

THanks friend..

I tried it was working now...

Thanks a lot..could you please provide your email ID to contact..

---------- Post updated at 09:37 AM ---------- Previous update was at 08:55 AM ----------

Hi Friend,

I was not working for me when first file found data in file then it's getting exit and completed it's not going to check for another file.

let me check

---------- Post updated at 09:41 AM ---------- Previous update was at 09:37 AM ----------

Hi Rudy,

I have implemented the logic given below.

a=/tjx/iisNAS/data/edw/fp/output/common/sample.txt
b=/tjx/iisNAS/data/edw/fp/output/common/sample1.txt
for file in $a $b
do if [ 0 -eq $(wc -l /tjx/iisNAS/data/edw/fp/output/common/sample.txt) && 0 -eq $(wc -l /tjx/iisNAS/data/edw/fp/output/common/sample1.txt)] && 
   then mailx ...
        exit 1
   else mailx ...
        exit 0
   fi
done

Discussion on unix.com is intended to stay on unix.com so that it provides benefit to future readers.

Why do you define the a and b variables, use them for the for loop, and then put in the filenames as fixed strings? And what is the && for at the end of the if line?
The reason for your complaint is called "shortcircuit evaluation" and is used in many languages: If the first operand in an and operation fails, the result is known and the second is not needed to be evaluated.

Hi Rudy,

Actually script given by you i executed and checked the result what it is doing checking the first file if data is there then getting executed exit command and coming out.Not checking for another file.

So I have prepared please review and correct me if anything wrong.

Thanks in advance..

Sorry, I don't get the logics behind your request nor from your code snippet. Please explain in plain English what you want to achieve. Are you aware of the short circuit evaluation?

Hi Rudy, I have executed the below script but it's checking the first file and if data found infile then it's getting executed exit 0 and coming out side without check the second file. a=/tjx/iisNAS/data/edw/fp/output/common/sample.txtb=/tjx/iisNAS/data/edw/fp/output/common/sample1.txtfor file in $a $bdo if [ 0 -eq $(wc -l <$file) ] then mailx ... exit 1 else mailx ... exit 0 fidoneCan you please verify and correct me if anything worng.

Thanks,

Did you read (and understand) post 7 ?

And, besides, why don't you use code tags as requested?

Yes..Correct..

There may be some of the files which has zero records(Ex:First file has data..second file doesn't have data...) when it checks for first file it's gone through but second file doesn't have data as per my consern second file also has to be checked.
But it's coming out from for loop.

Ok..Anyhow thanks for your response...

Have a great day...

Then you have to rephrase the logics. Check both files, keeping results in variables, then test the variables and proceed depending on test results.

Ok..
I will check it Rudy..and prepare script..

Thanks in advance..