Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ksh scripting 1

Status
Not open for further replies.

jprabaker

Technical User
May 31, 2001
185
GB
I am trying to write a section of script that tests a variable to see if it matches 1 of 2 things, if not then loop until it does. I am trying the following syntax but with no luck:

read VAR
while test \($VAR -ne ABC || $VAR -ne XYZ\)
do
print "\nDoh..! Try again...."
read GASAPP
done

I have tried messing around with []'s but still have no luck. Can someone please tell me what I'm doing wrong?


Thanks in advance

John
 
Couple of things...

read VAR
while [ ! "$VAR" = "ABC" -a ! "$VAR" = "XYZ" ]
do
print "\nDoh..! Try again...."
read GASAPP
done

This works.

Your version has "-ne". This only works for numeric tests. Also, the Demoregan (sp) rule of (logic) negation bit ya. You have to be careful when mixing NOT/AND/OR together. The NOT changes the expected results of the AND/OR. I have tested my code snippet on AIX 4.3.3 on an F50 and it works fine.

I'm sure there are other ways to code this, so here is only 1 suggestion. I'm sure there will be others.

Bill.
 
Yep,

Indeed that works very well.

Much appriciated
 
Of course the other option is to use the "until" statement, which may read better in this case i.e. keep looping until variable equals value.

This would also get rid of using the negate ! function as well, increasing readability.

read VAR
until [ "$VAR" = "ABC" -a "$VAR" = "XYZ" ]
do
print "\nDoh..! Try again...."
read VAR
done

Why have you opted for a different variable name within the do...done loop as well - this variable name isn't being tested in the [...] construct (hence I've changed this in the script.

As ever scripting is a personal matter, but I hope the above helps.

Cheers.
Dave Vickers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top