TransWikia.com

Infinite while loop issue using read

Unix & Linux Asked by Mister123 on December 15, 2021

I am writing a small script that asks a user to insert a number from 1-10. The script will then tell the user if the number is between the requested values, and continue from there.

However, I am having an issue trying to get the script to read back to the screen if the value is less than 1 or greater than 10. Each time the script is executed, whether correct or not, the script will exit and go to the echo statement after “done”.

I am trying to create an “infinite loop” if the user keeps inputting the incorrect value.

The echo statement after “done” is the second part of my script, however that isn’t the part I am having issues with.

Thank you for any help provided.

Script:

echo "Please type a number between 1-10."
read insertnum
while [ "$insertnum" -ge 1 -a "$insertnum" -le 10 ]
 do
    if [ "$insertnum" -ge 1 -a "$insertnum" -le 10 ]
     then
# Prompt the user that their answer is acceptable
     echo "Your answer is between 1-10"
     echo
     break
    else
# Prompt the user that their answer is not acceptable
     echo "Your number is not between 1-10."
     echo
     echo "Please type a number between 1-10."
     read insertnum
     echo
fi
done
echo "We will now do a countdown from $insertnum to 0 using a for loop."

4 Answers

I'd write it:

min=1 max=10
until
  printf "Please type a number between $min-$max: "
  IFS= read -r insertnum
  [ "$insertnum" -ge "$min" ] && [ "$insertnum" -le "$max" ]
do
  echo "Your number is not between $min-$max."
done
echo "Your answer is between $min-$max"
echo
echo "We will now do a countdown from $insertnum to 0 using a for loop."

Answered by Stéphane Chazelas on December 15, 2021

A shorter script:

unset a
until  [ "$a" = 1 ]
do     read -p "Please type a number between 1-10: " insertnum
       : $(( a=( insertnum > 0 )&( insertnum < 11 ) ))
       printf 'Your number is %.'"$a"'0sbetween 1-10.nn' 'not '
done
echo "We will now do a countdown from $insertnum to 0 using a for loop."

Answered by user79743 on December 15, 2021

while true ; do
  read -p "Please type a number between 1-10: " insertnum
  if [ "${insertnum}" -ge 1 ] && [ "${insertnum}" -le 10 ]
    then
      echo -e "acceptable answer between 1 and 10nnn"
      break
    else
      echo -e "your answer is unacceptable. It has to be be between 1 and 10nnn"
  fi
done
echo "We will now do a countdown from ${insertnum} to 0 using a for loop."

Answered by MelBurslan on December 15, 2021

This should work:

read -p "Please type a number between 1-10: " insertnum
while true; do
    if [ "$insertnum" -ge 1 ] && [ "$insertnum" -le 10 ];then
        # Prompt the user that their answer is acceptable
        echo "Your answer is between 1-10"
        echo
        break
    else
        # Prompt the user that their answer is not acceptable
        echo "Your number is not between 1-10."
        echo
        read -p "Please type a number between 1-10: " insertnum
    fi
done
echo "We will now do a countdown from $insertnum to 0 using a for loop."

I would use shell functions, at least for asking the number to the user, as this piece of code will be executed multiple times if user enters a digit outside valid range...Programming mantra: Don't repeat yourself.

Answered by marc on December 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP