Hi all, i am writing a shell script in which i got stuck when tried to use a if statement.
This is my code :
#!/bin/sh
today=`date +"%a %b %e"`
find /prod/logs -name '*.log' -mtime 0 -exec cp {} /home/hzjnr0/test \;
find /home/hzjnr0/test -type f -exec grep -i "successfully" {} \; > /home/hzjnr0/success.txt
#mailx -s "Control-M job success status" abc@xyz.com < /home/hzjnr0/success.txt
grep -i "successfully" success.txt|wc -l
if
[ -lt 29 ]
then
cd /home/hzjnr0/test
for file in *.log
do
grep -iq 'successfully' $file
[ $? -ne 0 ] && echo "$file -> didnot ended successfully for $today" > test.txt
done
#mailx -s "Control-M job failure status" abc@xyz.com < /home/hzjnr0/test/test.txt
fi
The
grep -i "successfully" success.txt|wc -l will give me a value which i need to compare with 29 in the IF staement condition.
IF the condition is false then it will go to the next loop.
Can anybody help me out here.
x=`grep -ci successfully success.txt`
if [ $x -lt 29 ]
then
.
.
else
continue
fi
Remember, the if-block should be inside some loop for this to work
Hi Balajesuri,
I am getting this error while trying with your code
new.sh[8]: test: Specify a parameter with this command.
gary_w
4
Are you sure you specified the "c" option to grep to count lines? Your error indicates $x is null. It should be 0 or greater.
Yes i have used the -c option..
here is the o/p and error i am getting
$ sh -x new.sh
+ + date +%a %b %e
today=Tue Jul 24
+ find /prod/logs -name *.log -mtime 0 -exec cp {} /home/hzjnr0/test ;
+ find /home/hzjnr0/test -type f -exec grep -i successfully {} ;
+ 1> /home/hzjnr0/success.txt
+ grep -ci successfully success.txt|wc -l
+ x=
new.sh[6]: grep -ci successfully success.txt|wc -l: not found.
+ [[ -lt 29 ]]
+ cd /home/hzjnr0/test
ec01
6
From the error it looks like you may not be in the correct/same directory as success.txt at that point. Also, get rid of wc -l if you're using grep -c
#!/bin/sh
today=`date +"%a %b %e"`
find /prod/logs -name '*.log' -mtime 0 -exec cp {} /home/hzjnr0/test \;
find /home/hzjnr0/test -type f -exec grep -i "successfully" {} \; > /home/hzjnr0/success.txt
#mailx -s "Control-M job success status" abc@xyz.com < /home/hzjnr0/success.txt
if [ `grep -ic "successfully" /home/hzjnr0/success.txt` -lt 29 ]
then
cd /home/hzjnr0/test
for file in *.log
do
grep -iq 'successfully' $file || ( echo "$file -> didnot ended successfully for $today" > test.txt )
done
#mailx -s "Control-M job failure status" abc@xyz.com < /home/hzjnr0/test/test.txt
fi
1 Like