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

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

simplified coding:

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

if [ $ans != "s" ] && [ $ans != "S" ]

Might be clearer to change it from negative to positive logic by using until, and use expr to shorten the code a little:

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:

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

Thank you all for helping me out mine was a just logic problem :o

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

I thought I already answered that question??

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:

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

Hi All
Thank you for your replies.will try and get back to you
Thanks

Thanks for your answers i will check and get back to you:b:

Hi All
Thanks for your answers but unfortunately both until and case are not solving my purpose
Anyhow i can manage with while loop now
Thanks
Suresh