Results 1 to 6 of 6
echo "Would you like to continue? [Y/N]"
read answer
if [ "$answer" != "y" ] || [ "$answer" != "Y" ]; then
echo "Elected not to continue, aborting"
exit 1
...
- 02-02-2009 #1Just Joined!
- Join Date
- Jan 2009
- Posts
- 17
sh conditional statements
I feel like a complete noob... but this is only giving me "Elected not to continue, aborting"echo "Would you like to continue? [Y/N]"
read answer
if [ "$answer" != "y" ] || [ "$answer" != "Y" ]; then
echo "Elected not to continue, aborting"
exit 1
fi
I can echo $answer and it is correct. Using sh shell.
- 02-02-2009 #2Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695
Google > bash scripting
* Edit: Your logic is flawed...If the answer is "y", then your statement is true. If the answer is "Y", then it's true. And if it's "P", it's also true.Code:if [ "$a" -gt 0 ] && [ "$a" -lt 5 ] then echo "The value of \"a\" lies somewhere between 0 and 5." fi
- 02-02-2009 #3Just Joined!
- Join Date
- Jan 2009
- Posts
- 17
I want it to be true if I give it anything other than y OR Y.
SO I give it y. $answer == y so the condition is false ($answer != y), thus my script continues to run. If i give it p, it is not "y" thus true, and i exit.
But that is not happening so something prolly is wrong with my logic or syntax. Can you spot it in my code or provide an improved logiv statement for what im doing?
Thanks so far.
- 02-02-2009 #4Just Joined!
- Join Date
- Jan 2009
- Posts
- 17
Is this problem a limitation of sh? SHould I be using bash instead?
- 02-02-2009 #5Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695
Truth Tables
P => $A != "y"The proposition "p or q" is false when both p and q are false and is true all other times.
Q => $A != "Y"
This is TRUE *all* times except when both P and Q are false. What letter could make both of these tests false? There is none. This will always be true.
$A = "Z" => P (True), Q (True)
$A = "y" => P (False), Q (True)
$A = "Y" => P (True), Q (False)
- 02-02-2009 #6Just Joined!
- Join Date
- Jan 2009
- Posts
- 17
Okay. Thanks. Thats kind of scary how long it took me to see the flaw in my logic.
So if I replace my OR with an AND it will work. How dumb can i be!
Thanks for the help.


Reply With Quote