|
|||||||
| Forums | Search Forums | Register | Forum Rules | Man Pages | Albums | FAQ | Members | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
|
|
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
Try this !
if [ $i = "+" -o $i = "-" -o $i = "/" -o $i = "%" ]; then $x=$vr1 else print "You have entered an invalid option." |
| Sponsored Links | ||
|
|
#3
|
|||
|
|||
|
Just to add:.... and is represented by "-a".
|
|
#4
|
|||
|
|||
|
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 | ||
|
![]() |
| Thread Tools | Search this Thread |
| 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 |
|
|