![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
#1
|
|||
|
|||
|
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 |
| Forum Sponsor | ||
|
|
|
#2
|
||||
|
||||
|
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 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" ] |
|
#3
|
|||
|
|||
|
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
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
|
|||
|
|||
|
Thank you
Thank you all for helping me out mine was a just logic problem
|
|
#5
|
|||
|
|||
|
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 |
|
#6
|
|||
|
|||
|
I thought I already answered that question??
|
|
#7
|
|||
|
|||
|
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
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 |
|||
| Google The UNIX and Linux Forums |