multiple conditions in if statements | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

multiple conditions in if statements

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-23-2002
tim mauger tim mauger is offline
Registered User
 
Join Date: Apr 2002
Last Activity: 23 April 2002, 5:33 PM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question multiple conditions in if statements

Hi all, I'm confused about the proper syntax for multi-conditional if then statements. I'm trying to set limitations on info input on the command line.. i.e.

if [$x=[+|-|/|*|%]] ;then
$x=$vr1
else
print "You have entered an invalid option."

Can someone please clue me in on what is wrong with my syntax; what teh correct syntax is for using "and" "or" in similar statements.
Sponsored Links
    #2  
Old 04-24-2002
Deepa Deepa is offline
Registered User
 
Join Date: Apr 2002
Last Activity: 4 October 2007, 12:33 AM EDT
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Try this !

if [ $i = "+" -o $i = "-" -o $i = "/" -o $i = "%" ]; then
$x=$vr1
else
print "You have entered an invalid option."
Sponsored Links
    #3  
Old 04-28-2002
manish manish is offline
Registered User
 
Join Date: Apr 2002
Last Activity: 31 July 2002, 9:00 AM EDT
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Just to add:.... and is represented by "-a".
    #4  
Old 04-28-2002
Jimbo
Guest
 
Posts: n/a
On assignments to a variable, do x= instead of $x= .

When you use a mixture of -o and -a, you might have to control precedence. In the following, the first line would test true because -o has precedence:
if [ a = a -o b = b -a c = C ]
But add parentheses (you would need to backslash these) and it would now test false:
if [ ( a = a -o b = b ) -a c = C ]

Also, when checking for several values, a case statement makes for nice clean code, and even more handy when each value will result in a different action:

Code:
case $x in
 +|-|/|%) x=$vr1;;
       *) print "You have entered an invalid option."
          exit 1;;
esac

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
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
if then statements with two conditions...? audiophile Shell Programming and Scripting 3 09-19-2008 02:42 PM
multiple conditions in if/then grandtheftander UNIX for Dummies Questions & Answers 4 07-21-2006 01:58 PM



All times are GMT -4. The time now is 12:17 AM.