Find the answer to your Linux question:
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 ...
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Posts
    17

    sh conditional statements

    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 feel like a complete noob... but this is only giving me "Elected not to continue, aborting"

    I can echo $answer and it is correct. Using sh shell.

  2. #2
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Google > bash scripting

    Code:
    if [ "$a" -gt 0 ] && [ "$a" -lt 5 ]
    then
      echo "The value of \"a\" lies somewhere between 0 and 5."
    fi
    * 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.

  3. #3
    Just 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.

  4. #4
    Just Joined!
    Join Date
    Jan 2009
    Posts
    17
    Is this problem a limitation of sh? SHould I be using bash instead?

  5. #5
    Linux Guru
    Join Date
    Nov 2007
    Posts
    1,695
    Truth Tables

    The proposition "p or q" is false when both p and q are false and is true all other times.
    P => $A != "y"
    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)

  6. #6
    Just 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...