0

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 4

    2

    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." 
      0

      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.

        0
        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 10\n\n\n" break else echo -e "your answer is unacceptable. It has to be be between 1 and 10\n\n\n" fi done echo "We will now do a countdown from ${insertnum} to 0 using a for loop." 
          0

          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.\n\n' 'not ' done echo "We will now do a countdown from $insertnum to 0 using a for loop." 

            You must log in to answer this question.

            Start asking to get answers

            Find the answer to your question by asking.

            Ask question

            Explore related questions

            See similar questions with these tags.