LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 12-03-2012, 02:50 AM   #1
cnitin
LQ Newbie
 
Registered: Mar 2012
Posts: 25

Rep: Reputation: Disabled
if loop with multiple conditions


Hi Friends,

i've written a bash script which checks a log file for error codes and then sends mail accordingly.

I have one if condition whose pseudo code would be like below

if $a = m or $a = n or $a = o
then
do something
else
do something
fi

Code:
if [[ "$input1" == "ORA-01555" ]] ||  [[ "$input1" == "ORA-12012" ]] || [[ "$input1" == "ORA-20001" ]] || [[ "$input1" == "ORA-06512" ]
however above seems to be not working , other than || i've also used "-o" in if condition.

Kindly help

Last edited by cnitin; 12-03-2012 at 02:51 AM.
 
Old 12-03-2012, 02:56 AM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,453

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
Please, translate "above seems to be not working". It works for me. You can also put the logical OR inside the brackets:
Code:
if [[ "$input1" == "ORA-01555" ||  "$input1" == "ORA-12012" || "$input1" == "ORA-20001" || "$input1" == "ORA-06512" ]]
then
  echo do something
else
  echo do something else
fi
 
1 members found this post helpful.
Old 12-03-2012, 04:44 AM   #3
cnitin
LQ Newbie
 
Registered: Mar 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
Thanks Colucix,

i'm posting the code as below.

Code:
#!/bin/bash

expdate=`date '+%d_%m_%Y_%H_%M_%S'`

tail -300 /oraclehome/ora10g/admin/dbname/bdump/alert_dbname.log > /tmp/alrtdbname.log

> /tmp/errlinenum.txt
> /tmp/alrterr.txt

count=0
#Check the alert log for errors
while read inputline
        do
                echo $inputline > test1.txt
                count=$((count+1))
                input=`cat test1.txt`
                input1=`cat test1.txt |awk '{ print $1 }'`
                 if [[ "$input1" == "ORA-01555" || "$input1" == "ORA-12012" || "$input1" == "ORA-20001" || "$input1" == "ORA-06512" ]]
                then
                        echo "Error can be ignored as it is caused by an SQL statement or a JOB"
                        ##exit from script
                else
                        input2=`echo $input1 | cut -b -4`
                        if [[ "$input2" == "ORA-" ]]
                        then
                                echo "error found"
                                echo "Error at line number in errlinenum.txt "$count
                                errlinenum=$count
                                echo $errlinenum >> /tmp/errlinenum.txt
                        else
                                echo $input2 > /dev/null
                        fi
                fi
done < /tmp/alrtdbname.log

#Check if file is empty or not
if [ -s /tmp/errlinenum.txt ]
        then
                echo "Error in alertlog file at" $expdate

                while read inputline2
                        do
                        sed -n "$inputline2"p /tmp/alrtdbname.log >> /tmp/alrterr.txt
                done < /tmp/errlinenum.txt

       
        else
                echo "No error in alertlog file at" $expdate
fi
the if condition is not working as desired , it echoes

Code:
error found
Error at line number in errlinenum.txt <some number>
 
Old 12-04-2012, 06:54 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
You still haven't define 'not working'
This will show you what its really doing
Code:
#!/bin/bash

#Tell parser to show us before and after version of each line
# and result
set -xv


<rest of your code>
 
1 members found this post helpful.
Old 12-05-2012, 09:29 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
I see this thread is now marked as solved. I take it that the above suggestions worked for you then? General etiquette recommends that you should acknowledge it explicitly with a follow-up post.

I have a few additional suggestions concerning your script:

1) $(..) is highly recommended over `..`

2) QUOTE ALL OF YOUR VARIABLE EXPANSIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes

When you have longer strings, the usual rule of thumb is to simply quote the longest string possible, including the variables.

Code:
#echo "Error at line number in errlinenum.txt "$count
echo "Error at line number in errlinenum.txt $count"
3) Avoid the Useless Use Of Cat. Tools like awk can generally accept files as direct arguments.

4) If your purpose is to test a single string against multiple conditions, a case statement is usually preferred over multiple ifs.
Code:
case $input1 in
	ORA-01555|ORA-12012|ORA-20001|ORA-06512)
		echo "Error can be ignored as it is caused by an SQL statement or a JOB"
	;;
	ORA-*) echo "error found"
		...
	;;
	*)  echo $input2 > /dev/null
	;;
esac
(What's the purpose of that last command, BTW, since it basically does nothing? If you need a placeholder command, use ":" (a synonym for the "true" command), or else just leave it out of the script.

5) If a string is already stored in a variable, it's usually recommended to use parameter substitution or other built-in string manipulation techniques, rather than external commands.

Code:
#input2=`echo $input1 | cut -b -4`
input2=${input1:0:4}
You can also use globbing patterns directly in double-bracket tests.

Code:
if [[ $input1 == ORA-* ]]
Although in this particular situation you should be able to just include the it in the enclosing case statement, as I demonstrated above.

6) When using bash or ksh, it's recommended to use [[..]] for string/file tests, and ((..)) for numerical tests. Avoid using the old [..] test unless you specifically need POSIX-style portability.

http://wiki.bash-hackers.org/commands/classictest
http://wiki.bash-hackers.org/syntax/...nal_expression
http://wiki.bash-hackers.org/syntax/arith_expr

I'm recommending this here particularly to highlight that you should be consistent in their use.
 
1 members found this post helpful.
Old 12-09-2012, 11:42 PM   #6
cnitin
LQ Newbie
 
Registered: Mar 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
Hi David and Chrism01 ,

It was just my own mistake ,

The output using awk was different than what i expected , and when i would compare it in if loop it would always turn false and the loop would end in else block.

when i tried awk on the logfile , i saw that the awk was returning "ORA-0512: " that colon was the whole issue , my mistake ..

Thanks a lot guys , and david and chrism01 ,the tips that you gave would sure be helpful to me in future.

have fun .

Last edited by cnitin; 12-09-2012 at 11:44 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple conditions in while() loop in C glosterj9 Programming 1 12-27-2011 11:16 AM
egrep with multiple regex conditions c_mitulescu Programming 2 07-08-2009 04:53 AM
Multiple If conditions keysorsoze Programming 4 12-12-2007 01:07 AM
Bash while loop with 2 conditions. elinenbe Programming 2 10-14-2007 12:06 AM
shell script - while loop with multiple conditions ronsha Programming 13 12-10-2005 04:08 PM


All times are GMT -5. The time now is 09:07 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration