Problem in if condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem in if condition
# 1  
Old 02-21-2010
Problem in if condition

Hi all,

I have task to delete two different files from all file system. one is core file & other is old file.
i can delete core file but for old file i have to mv in different location.
i wrote a script but it is not working.
i have a two variables in this script first one is delcnt & delcnt_old.
i want to send mail if there is any value except 0 in these two variables.
like if 1st variable is having 0 and other one is 1 that mail should be trigger means
if there is any one condition it should work.
===============
Code:
if [ $delcnt !=0 ] && [$delcnt_old != 0]
 
 
#!/bin/ksh
print '================================================'>> log_core
delcnt=0
delcnt_old=0
for files in `find ./ -name core_test -mtime -5 -type f -print`
do
echo " $files  Deleted : " `date +%Y-%m-%d.%Hh%Mm%Ss` >> log_core
delcnt=$(($delcnt + 1))
/bin/rm $files
done
for oldfiles in `find ./ -name core_test_old -mtime -5 -type f -print`
do
echo " $oldfiles moved : " `date +%Y-%m-%d.%Hh%Mm%Ss` >> log_core
delcnt_old=$(($delcnt_old + 1))
/usr/bin/mv $oldfiles /usr/sap/RS2/DVEBMGS35/core_new/core_test
done
echo "deleted $delcnt files">> log_core
echo "deleted $delcnt_old files">> log_core
         print '====================End==========================='>> log_core
            if [ $delcnt !=0 ] && [$delcnt_old != 0]
            then
            cat /usr/sap/RS2/email_list | while read emails
            do
            echo $emails
            mailx -r alert -s "Core Deletion" $emails<log_core && mailq
            echo Mail Sent Successfully
            done
            fi

====================

Please help
Ravi Kumar
Thanks & regards.

Last edited by DukeNuke2; 02-21-2010 at 04:50 AM..
# 2  
Old 02-21-2010
Try changing the if part like this...
Code:
if [[ $delcnt -ne 0  && $delcnt_old -ne 0 ]];
then
#bla bla bla
fi

# 3  
Old 02-21-2010
Thanks sir,
it is working ........

thanku very much

with regards,
Ravi Kumar
# 4  
Old 02-21-2010
or
Code:
if ((delcnt <> 0 && delcnt_old <> 0 ))
then
  ...
fi

Code:
if [ "$delcnt" -ne 0 -a "$delcnt_old" -ne 0  ]
then
  ...
fi

; not needed in those format.
But if then is in same line as if, then you need ; between commands if and then
# 5  
Old 02-21-2010
Data

Hi,
I changed my script as you mentioned in your earlier suggestion.
but when first condition is means "delcnt" is more then 0 mail is triggered
but when 2nd condition "delcnt_old=0" is above then 0 it is not working. means second condition is not working means mail is not triggering.

kinly suggest.



Code:
#!/bin/ksh
print '================================================'>> log_core
delcnt=0
delcnt_old=0

for files in `find ./ -name core_test -mtime -5 -type f -print`

do

echo " $files  Deleted : " `date +%Y-%m-%d.%Hh%Mm%Ss` >> log_core
delcnt=$(($delcnt + 1))
/bin/rm $files
done

for oldfiles in `find ./ -name core_test_old -mtime -5 -type f -print`

do

echo " $oldfiles moved : " `date +%Y-%m-%d.%Hh%Mm%Ss` >> log_core
delcnt_old=$(($delcnt_old + 1))
/usr/bin/mv $oldfiles /usr/sap/RS2/DVEBMGS35/core_new/core_test
done


echo "deleted $delcnt files">> log_core
echo "deleted $delcnt_old files">> log_core

         print '====================End==========================='>> log_core

            if [[ $delcnt -ne 0 && $delcnt_old -ne 0 ]];
            then
            cat /usr/sap/RS2/email_list | while read emails
            do
            echo $emails
            mailx -r alert -s "Core Deletion" $emails<log_core && mailq
            echo Mail Sent Successfully
            done
            fi            rm log_core



---------- Post updated at 05:18 AM ---------- Previous update was at 03:48 AM ----------

Quote:
Originally Posted by dravi_laxmi
Hi,
I changed my script as you mentioned in your earlier suggestion.
but when first condition is means "delcnt" is more then 0 mail is triggered
but when 2nd condition "delcnt_old=0" is above then 0 it is not working. means second condition is not working means mail is not triggering.

kinly suggest.



Code:
#!/bin/ksh
print '================================================'>> log_core
delcnt=0
delcnt_old=0
 
for files in `find ./ -name core_test -mtime -5 -type f -print`
 
do
 
echo " $files  Deleted : " `date +%Y-%m-%d.%Hh%Mm%Ss` >> log_core
delcnt=$(($delcnt + 1))
/bin/rm $files
done
 
for oldfiles in `find ./ -name core_test_old -mtime -5 -type f -print`
 
do
 
echo " $oldfiles moved : " `date +%Y-%m-%d.%Hh%Mm%Ss` >> log_core
delcnt_old=$(($delcnt_old + 1))
/usr/bin/mv $oldfiles /usr/sap/RS2/DVEBMGS35/core_new/core_test
done
 
 
echo "deleted $delcnt files">> log_core
echo "deleted $delcnt_old files">> log_core
 
         print '====================End==========================='>> log_core
 
           if [[ $delcnt -ne 0 && $delcnt_old -ne 0 ]];
           then
           cat /usr/sap/RS2/email_list | while read emails
           do
           echo $emails
           mailx -r alert -s "Core Deletion" $emails<log_core && mailq
           echo Mail Sent Successfully
           done
           fi            rm log_core

# 6  
Old 02-21-2010
Ofcourse, if one of the two variables are 0, it won't send email. But as i understand...you want to send email either one of the two variables are more than 1. In this case, use Or(||) not And(&&)...

Code:
if [[ $delcnt -ne 0  ||  $delcnt_old -ne 0 ]]
then
#bla bla
fi

# 7  
Old 02-22-2010
Code:
delcnt_old=$(($delcnt_old + 1))
# or
(( delcnt_old=delcnt_old + 1 ))
# or more C like
(( delcnt_old+=1 ))

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Condition problem

Hi All, Seeking for your assistance on how to condition it correctly. cat file1.txt 290,1663,43,888,0,0.00,86.91,0.00,26.98,0.00 290,1663,52,0,0,0.00,0.00,0.00,0.00,0.00 290,1663,52,888,0,0.00,34.60,0.00,9.00,0.00 1st scenario: if the fourth column contains 888s and 0s it is by... (16 Replies)
Discussion started by: znesotomayor
16 Replies

2. Shell Programming and Scripting

If condition problem

Hi All, I am using below if condition to check whether null is passed as a parameter to the program if or ; then echo "ABC">>$FILE else echo "CDF">>$FILE fi However it is saying me null=null command not found . Please help me with this (9 Replies)
Discussion started by: Hypesslearner
9 Replies

3. Shell Programming and Scripting

Problem with IF condition .

Hi i am writing a script where i am running , 5 scripts together in 1 script . Now what i want is when these 5 scripts run completely , i should execute some other commands like i have compile the data etc. I have have 5 echo statements at the end of all those scripts . Like echo "1 is done" in... (1 Reply)
Discussion started by: honey26
1 Replies

4. Shell Programming and Scripting

If condition problem

Hi, I need to use if condition for search a file pattern on a particular location. cd $file_Path if || then do this else do that fi Can someone help me with the if part, how i can put those conditions? make sure format should be *.file* and *.file file is a keyword which i... (5 Replies)
Discussion started by: amit.mathur08
5 Replies

5. Shell Programming and Scripting

Problem in using AND OR condition together

a=rhino b=crocodil c=testsc if && "$c" = testsc ] then echo "Test #5 succeeds." else echo "Test #5 fails." fi i need to test or condition before check the output with AND condition. ur help is much appreciated... (11 Replies)
Discussion started by: gokulraj23
11 Replies

6. Shell Programming and Scripting

problem in if then else condition

Hi , I am trying the following simple script . But it is always giving 1 output. Dont know why #!/bin/sh find . -name "a.log" if ; then echo "1" else echo "0" fi Kindly advice. it is giving 1 output even when the a.log file is not there (26 Replies)
Discussion started by: himvat
26 Replies

7. Shell Programming and Scripting

problem in if condition

hi, actully i need the belp for the below. host_list=" Host1 host2 host3 host4 " n=`hostname` i need to put the condition like the below if n is among the host mention in the host_list if then #some stugg else # some other stuff fi (1 Reply)
Discussion started by: mail2sant
1 Replies

8. Shell Programming and Scripting

problem with if condition

hi, :) pls consider the following if statement if //g') ] then ........ else ....... when i execute the script i am getting the following error '(' unexpected I am not able to find the mistake. could anybody tell where i did mistake. cheers RRK (13 Replies)
Discussion started by: ravi raj kumar
13 Replies

9. Shell Programming and Scripting

If condition problem

Hi Guys, I want to use if conition for my script. Before I used it tried it with some small test scripts. But it was not succeeded. My script and screen output as follows, Script: echo 'Do you think Yes or No (y/n) : ' read ans echo You input anser as $ans ans1=y if ( $ans == $ans1... (5 Replies)
Discussion started by: maheshsri
5 Replies
Login or Register to Ask a Question