Need a clue to write a IF statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a clue to write a IF statement
# 1  
Old 07-24-2012
Java Need a clue to write a IF statement

Hi all, i am writing a shell script in which i got stuck when tried to use a if statement.

This is my code :
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.

Last edited by Franklin52; 07-24-2012 at 08:46 AM.. Reason: Please use code tags for data and code samples, thank you
# 2  
Old 07-24-2012
Code:
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
# 3  
Old 07-24-2012
Hi Balajesuri,
I am getting this error while trying with your code


new.sh[8]: test: Specify a parameter with this command.
# 4  
Old 07-24-2012
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.
# 5  
Old 07-24-2012
Yes i have used the -c option..
here is the o/p and error i am getting


Code:
$ 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

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 07-24-2012 at 11:54 AM..
# 6  
Old 07-24-2012
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
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
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


Last edited by ec01; 07-24-2012 at 01:46 PM.. Reason: added reason for earlier error
This User Gave Thanks to ec01 For This Post:
# 7  
Old 07-25-2012
Thanks. it worked. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk/sed problem to write Db insertion statement

Hi There, I am trying to load data from a csv file into a DB during our DB migration phase. I am successfully able export all data into a .csv file but those have to rewritten in terms insert statement which will allow for further population of same data in different DB My exiting csv record... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

2. Solaris

Filesystem filling up and no clue as to why!

df shows that the filesystem is filling up and the usage is 94%. However when I actually traverse to the directory I du shows only about 10% of the space occupied! Below is the output of df and du: >>>df -kh /cbmdata/00 470M 393M 29M 94% /cbmdata/00 >>>/cbmdata/00>... (3 Replies)
Discussion started by: zombiezparadize
3 Replies

3. Shell Programming and Scripting

If statement - How to write a null statement

In my ksh script, if the conditions of a if statement are true, then do nothing; otherwise, execute some commands. How do I write the "do nothing" statement in the following example? Example: if (( "$x"="1" && "$y"="a" && "$z"="happy" )) then do nothing else command command fi... (3 Replies)
Discussion started by: april
3 Replies

4. Shell Programming and Scripting

write statement into one file

I have one file name start.sh in rinku directory. I want to move it to rinku1 directory. I also mantain log file. In the log file I want to write the the statement whenever the move is failed. Example: >echo `mv /rinku/me.tx ../rinku1` | tee -a log1 mv: me.tx: cannot access: No such file or... (5 Replies)
Discussion started by: rinku
5 Replies

5. Shell Programming and Scripting

How to write an OR statement

Hi all, I want to check if the first or the second or the third argument of call to a shell script is empty. How do i do that in a short way. I watched some examples and i believe if checks if an argument is NULL. If so, how can i add the checks for argument 2 and 3 in the statement? ... (1 Reply)
Discussion started by: aukequist
1 Replies
Login or Register to Ask a Question