The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


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 !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can i put the condition in for loop for the below. mail2sant Shell Programming and Scripting 1 04-28-2008 01:49 AM
Testing For Loop condition mavsman UNIX for Dummies Questions & Answers 4 04-01-2008 08:45 AM
Checking condition inside the loop ithirak17 Shell Programming and Scripting 1 03-13-2008 05:37 AM
What condition to be put in the while loop? nehaquick UNIX for Dummies Questions & Answers 1 02-15-2008 01:06 PM
End of loop condition required??? skyineyes Shell Programming and Scripting 4 07-16-2007 12:10 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-07-2008
Registered User
 

Join Date: Aug 2008
Posts: 26
while loop not taking the not equal to condition

Hi
I am trying to write a code like this
echo "enter a type"
read a_type
while [ $a_type != "S" ] || [ "$a_type" != "s" ] || [ "$a_type" != "SR" ] || [ "$a_type" != "sr" ] || [ "$a_type" != "a" ] || [ "$a_type" != "A" ]
do
echo "invalid engine type Please enter correct a_type"
read engine_type < /dev/tty
done

My problem is that even if i give the a_type as the correct one that i have listed above that is S or s so on and so forth it is still entering in the loop and telling invalid a_type
Instead if i give = condition in while it is working fine . That is if i give while [ $a_type = "S" ] then if i give S it is entering and not S it is not entering . But giving = dosent satisfy my code . I have to give not equal to if i want the code to work
Please help
Thanking in advance
Reply With Quote
Forum Sponsor
  #2  
Old 08-07-2008
joeyg's Avatar
Moderator
 

Join Date: Dec 2007
Location: Home of world champion Boston Celtics
Posts: 983
Question perhaps it is the logic?

You have or between each. I think you may want and logic.

simplified coding:
Code:
Enter a code
read ans
if [ $ans != "s" ] || [ $ans != "S" ]
 then
 echo "no more"
fi
Thus, enter "s"
if [ s != "s" ] || or [ s != "S" ]
becomes
if [ 0 ] || [ 1 ]
which is a 1

Often, when doing multiple conditions, != is with or while = is with and.
Thus, perhaps better if
Code:
if [ $ans != "s" ] && [ $ans != "S" ]
Reply With Quote
  #3  
Old 08-07-2008
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 920
Might be clearer to change it from negative to positive logic by using until, and use expr to shorten the code a little:

Code:
echo "enter a type"
read a_type
until expr "$a_type" : "[SsAa]" || expr "$a_type" : "[Ss][Rr]"
do
        echo "invalid engine type Please enter correct a_type"
        read a_type < /dev/tty
done
Or use grep:

Code:
echo "enter a type"
read a_type
until echo "$a_type" | grep -Eixq "[sa]|sr"
do
        echo "invalid engine type Please enter correct a_type"
        read a_type < /dev/tty
done
Reply With Quote
  #4  
Old 08-08-2008
Registered User
 

Join Date: Aug 2008
Posts: 26
Thank you

Thank you all for helping me out mine was a just logic problem
Reply With Quote
  #5  
Old 08-08-2008
Registered User
 

Join Date: Aug 2008
Posts: 26
I have another question related to my question posted yesterday

while [ "$engine_type" != "S" ] && [ "$engine_type" != "s" ] && [ "$engine_type" != "SR" ] && [ "$engine_type" != "sr" ] && [ "$engine_type" != "a" ] && [ "$engine_type" != "A" ]

is working fine
but how to short cut it say i mean the user can enter S or s A or a or Sr rS like that
writing everything in while makes it cumbersome
Is there any other way
Thanks in advance
Reply With Quote
  #6  
Old 08-08-2008
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 920
I thought I already answered that question??
Reply With Quote
  #7  
Old 08-08-2008
Bughunter Extraordinaire
 

Join Date: May 2005
Location: In the leftmost byte of /dev/kmem
Posts: 1,262
To be honest i think you should change your programs logic completely: You enter a character (or word), some values are legal, ALL others are not. You do not need the functionality of a while-loop at all, so why should you use it?

How about the following, which is easily extensible:

Code:
print - "enter a value: " ; read value
case value in
     [Ss])
          print - "You entered an s or an S."
          ;;

     [Nn])
          print - "You entered an n or an N."
          ;;

     *)
          print - "You entered an illegal value"
          ;;
esac
This should also be easier to read and to maintain.

If you want to use the code part as a device to process the commandline you could also resort to the getopts-program. Have a look at the manpage for it and if this fits your requirements and you still have problems applying it come back and ask again.

I hope this helps.

bakunin
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:08 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0