multiple conditions in if/then | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

multiple conditions in if/then

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-21-2006
grandtheftander grandtheftander is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 8 August 2006, 3:41 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
multiple conditions in if/then

Hello,
I am having trouble with the syntax with a conditional statement in a BASH script involving multiple conditions. Any suggestions would be greatly appreciated!

if [ "$sString" != "2" && "$sString" != "5" && "$sString" !="8
" && "$tString" !="3" && "$tString" != "5" ]; then
array=("${array[@]}" "$dnNum" )
fi
i receive this error:
./testscript: [: missing `]'
Sponsored Links
    #2  
Old 07-21-2006
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 30 July 2014, 10:30 AM EDT
Location: NM
Posts: 10,159
Thanks: 269
Thanked 781 Times in 731 Posts
Did you really have this on two lines like this?
If so put a \ character right after !="8"
Sponsored Links
    #3  
Old 07-21-2006
grandtheftander grandtheftander is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 8 August 2006, 3:41 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
they are infact on the same line, but i did try adding a '\'. I also tried the if statment with only two "conditions".
i.e.
f [ "$sString" != "2" && "$sString" != "5"]; then
array=("${array[@]}" "$dnNum" )
fi
    #4  
Old 07-21-2006
tmarikle tmarikle is offline Forum Advisor  
Registered User
 
Join Date: Jan 2005
Last Activity: 27 September 2011, 12:36 PM EDT
Posts: 683
Thanks: 0
Thanked 4 Times in 4 Posts
The last example is failing because of your "5" sitting next to the "]" with no whitespace.

Your first example will work if you replace "&&" with "-a" or if you replace your brackets "[" and "]" with double brackets "[[" and "]]".

Example:

Code:
if [ "$sString" != "2" -a "$sString" != "5" ]; then
...
fi

and:

Code:
if [[ "$sString" != "2" && "$sString" != "5" ]]; then
...
fi

Sponsored Links
    #5  
Old 07-21-2006
grandtheftander grandtheftander is offline
Registered User
 
Join Date: Jul 2006
Last Activity: 8 August 2006, 3:41 PM EDT
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Computer

thank you so much...this was the first time i've posted on a forum, and it def proved its usefulness...

Last edited by grandtheftander; 07-21-2006 at 02:20 PM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multiple indexed conditions with ksh vahid Shell Programming and Scripting 1 07-19-2011 10:23 PM
Nested if with multiple conditions sweetnsourabh UNIX for Dummies Questions & Answers 3 11-29-2010 05:47 AM
specifying multiple conditions in AWK skyineyes Shell Programming and Scripting 2 05-12-2010 10:31 AM
Help regarding multiple conditions ssenthilkumar Shell Programming and Scripting 4 01-08-2010 02:10 AM
multiple if conditions bashshadow1979 Shell Programming and Scripting 4 04-21-2009 03:08 PM



All times are GMT -4. The time now is 01:10 PM.