5

I tried many different combinations with brakets, quotation marks, ||, -o, but the only way the script works without infinit loop is without the OR in while loop comparison, like this: while [ $name != Jorge ]] ; do ... This is an example of an script that I want to run:

#!/bin/bash
echo "What is my name: "
read name

while [ $name != "Jorge" ] || [ $name != "Eduardo" ] ; do    
    echo "Not. Try again: "
    read name
done

echo "Well done!"
2
  • 1
    I suggest to replace || by && Commented Aug 13, 2017 at 6:24
  • 1
    Since you are using bash, you could use the regex operator also while [[ ! $name =~ ^(Jorge|Eduardo)$ ]]; do Commented Aug 13, 2017 at 6:32

2 Answers 2

7

You should use && instead of || in the condition of while statement.

You are trying to read a name from stdin and if that name is "Jorge" or "Eduardo", you are done. when putting it in condition of while, you want to continue in the loop when the name if not "Jorge" and the name is not "Eduardo".

your current condition says that continue in the loop if the name is not "Jorge" or the name is not "Eduardo". And the name cannot be both at the same time.

Sign up to request clarification or add additional context in comments.

2 Comments

Would not have realized I did this too, if you hadn't explained it so well. Thank you!
2

Using regex:

while [[ ! "$name" =~ ^(Jorge|Eduardo)$ ]]; do

or Bash extented globbing:

shopt -s extglob
while [[ "$name" != @(Jorge|Eduardo) ]]; do

2 Comments

-a is obsolete and should not be used, per the POSIX specification for test.
@chepner Removed from my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.