Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

  1. The problem statement, all variables and given/known data:

I have written a script for a dice game that: (1) tells user that each of the 2 die are 6 sided ( Spots=6 ); (2) asks the user to guess the value of the rolled dies, which would be in the range of 2 to 12 ( $guess_value ); (3) script generates a random roll of the 2 die ( $RANDOM % $Spots +1� ); (4) tell user the random value rolled ( $roll_value ); (5) compare user's guess ( $guess_value ) to random roll ( $roll_value ), telling user if values are equal, the game is won, otherwise, game is lost.

I, originally, wrote the script without verifying that the user enters a number within the range of 2 to 12, which works as intended (see #3 below). Then, I added an if...else loop to verify user's entry (i.e. ask for another entry if not in range), but do not attain the result expected if a number outside the range of 2 to 12 is entered.

Could someone please assist with this verification loop? Thank You.

  1. Relevant commands, code, scripts, algorithms:

Below is the script that I'm having an issue with:

  clear
  die1=0
  die2=0
  Spots=6
   
  echo �Hello.  Welcome to Random Roll.�
  echo �The object of Random Roll is to guess the value of the 2 die rolled.�
  echo �You will be rolling 2 die.  Each of the two die have 6 sides.�
   
  echo �Please enter a number between  2 and 12.�
                          #max side of 6 could give a minimum roll value of 2 and a maximum roll value of 12
              read guess_value
   
  echo �You have guessed that you will roll a� $guess_value
   
   
  #Sanitize number input to ensure number is 2 to 12
   
  if [[ $guess_value �lt 2 && $guess_value �gt 12 ]]
              then
              echo �Sorry.  Your guess is not within the range of 2 to 12.  Please enter a number in the range of 2 to 12.�
              else
              echo �You have guessed that you will roll a� $guess_value.�
  fi
   
  read guess_value
   
  echo �The dice have been rolled.�
  echo �Rolling�.�
  echo �       Rolling��
  echo �             Rolling��
   
  let �die1 = $RANDOM  %  $Spots +1�                                                                     #die 1 roll
  let �die2 = $RANDOM  %  $Spots +1�                                                                     #die 2 roll
   
   
  let roll_value=die1+die2
   
  echo �You have rolled� $roll_value
   
  if test $roll_value �eq $guess_value
              then
              echo �Congratulations.  You have won Random Roll.�
              else
              echo �Sorry.  You have lost Random Roll.  Thank you for playing.�
  1. The attempts at a solution (include all code and scripts):

The script works fine without the loop trying to verify that the user's guess is within the range of 2 to 12. That script is:

  clear
  die1=0
  die2=0
  spots=6
   
  echo �Hello.  Welcome to Random Roll.�
  echo �The object of Random Roll is to guess the value of the 2 die rolled.�
  echo �You will be rolling 2 die.  Each of the two die have 6 sides.�
   
  echo �Please enter a number between  2 and 24.�
                          #max side of 6 could give a minimum roll value of 2 and a maximum roll value of 12
              read guess_value
   
  echo �You have guessed that you will roll a� $guess_value
   
  echo �THE DICE ARE ROLLING...�
  echo �Rolling�.�
  echo �       Rolling��
  echo �             Rolling��
   
  let �die1 = $RANDOM  %  $Spots +1�                                                                     #die 1 roll
  let �die2 = $RANDOM  %  $Spots +1�                                                                     #die 2 roll
   
  echo �Die 1 value is $die1.�
  echo �Die 2 value is $die2.�
   
  let roll_value=die1+die2
   
  echo �You have rolled� $roll_value
   
  if test $roll_value �eq $guess_value
              then
              echo �Congratulations.  You have won Random Roll.�
              else
              echo �Sorry.  You have lost Random Roll.  Thank you for playing.�
  fi
  1. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):

Baker College of Auburn Hills (Auburn Hills, MI); U.S.A. Instructor: David Koppy; Lux211

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).

You should use the logical operator || instead of &&.

Good Morning Franklin52,

Thank you for the reply and solution. Omitting the && within the loop in question and adding the logical operator || instead, did the trick. Can't believe that the solution was so simple. Being new to bash scripting, I thought for sure that the loop structure was incorrect.

Again, Thank You.

Lauren

You're welcome.