while loop not taking the not equal to condition | 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 !!

while loop not taking the not equal to condition

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-07-2008
ssuresh1999 ssuresh1999 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 27 July 2011, 12:04 PM EDT
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old 08-07-2008
joeyg's Avatar
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 17 November 2014, 3:07 PM EST
Location: Out running a Marathon.
Posts: 2,337
Thanks: 73
Thanked 155 Times in 144 Posts
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" ]

Sponsored Links
    #3  
Old 08-07-2008
Annihilannic Annihilannic is offline Forum Advisor  
 
Join Date: May 2008
Last Activity: 28 October 2009, 7:03 PM EDT
Location: Sydney, Australia
Posts: 1,009
Thanks: 0
Thanked 2 Times in 2 Posts
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

    #4  
Old 08-08-2008
ssuresh1999 ssuresh1999 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 27 July 2011, 12:04 PM EDT
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you

Thank you all for helping me out mine was a just logic problem
Sponsored Links
    #5  
Old 08-08-2008
ssuresh1999 ssuresh1999 is offline
Registered User
 
Join Date: Aug 2008
Last Activity: 27 July 2011, 12:04 PM EDT
Posts: 53
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #6  
Old 08-08-2008
Annihilannic Annihilannic is offline Forum Advisor  
 
Join Date: May 2008
Last Activity: 28 October 2009, 7:03 PM EDT
Location: Sydney, Australia
Posts: 1,009
Thanks: 0
Thanked 2 Times in 2 Posts
I thought I already answered that question??
Sponsored Links
    #7  
Old 08-08-2008
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 23 November 2014, 9:01 AM EST
Location: In the leftmost byte of /dev/kmem
Posts: 4,337
Thanks: 45
Thanked 839 Times in 662 Posts
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
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
If not equal to then loop gmears Shell Programming and Scripting 5 04-24-2012 04:09 AM
Comparison and For Loop Taking Too Long hanie123 UNIX for Advanced & Expert Users 1 07-10-2009 08:50 AM
For Loop Taking Too Long hanie123 Shell Programming and Scripting 5 07-07-2009 12:37 PM
Infinite for loop is taking too much CPU usage % pa.chidhambaram Solaris 7 06-23-2009 01:45 PM
Problem taking input from file with for loop bash_in_my_head Shell Programming and Scripting 4 12-03-2008 05:08 PM



All times are GMT -4. The time now is 11:52 AM.