Help with accidental endless loop

I was practicing writing simple loops as I am a new bash user and I created this script, which turned out to be an endless loop where the echo output does not stop and I do not see where my mistake is.

 
#!/bin/bash
echo 'enter a number from 1 to 100'
read number
while [ "$number -le 100 && $number -ge 1" ] 
do      
      echo you entered a good value
done

Hi,

  • The quotes turn the expression $number -le 100 && $number -ge 1 into the string "$number -le 100 && $number -ge 1" . The result is testing whether the string is non-empty. So at any case those quotes should not be there
  • the expression $number -le 100 && $number -ge 1 is not valid syntax. The correct syntax would be [ $number -le 100 ] && [ $number -ge 1 ]
  • Even if the expression would be made valid, then inside the loop no new number is read, giving the user no chance to enter an new entry, so depending on what initial input value was read, either the loop is never entered, or it is run indefinitely.
  • One additional observation: there should be no empty line above the shebang ( #!/bin/bash ). #! really must be the first two characters in the file for them to have their special meaning.

Did you want an if ...... then ....... else ........ fi statement block instead of a loop?

Robin