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.
2. Relevant commands, code, scripts, algorithms:
Below is the script that I'm having an issue with:
3. 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:
4. 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).
Last edited by Don Cragun; 05-12-2014 at 05:08 AM..
Reason: Remove hundreds of FONT tags; add CODE and ICODE tags.
Simple Solution: Bash Script for Dice Game; Issue with if...else loop to verify user guess is within
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.
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.
When I run the following script at the bottom it say cards remaining=44...It should be=35.
Can anyone tell me what I'm doing wrong. I've spent hours trying to get this to work and I can't go any further until this part works. thank you in advance
Cogiz
#!/bin/bash
# Date="November, 2016"
#... (2 Replies)
I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would... (8 Replies)
Hi, I have written a script that allows me to repetitively play a music file $N times, which is specified through user input. However, if I want to exit the script before it has finished looping $N times, if I use CTRL+c, I have to CTRL+c however many times are left in order to complete the loop.... (9 Replies)
I wrote a script to give a user 5 guesses on what is my favorite color but I it doesn't work. I've only been scripting for a couple weeks and need some help it seems simple but how do I give the user 5 guesses? (3 Replies)
Hi,
I have data in the following form:
AB001 10
AB002 9
AB003 9
etc
AB200 5
What I need to do is sum up the second value according to groups of the first, i.e. AB001 to AB030 the total being X, AB031 to AB050 the total being Y etc (there are 5 AB ranges of different sizes). I'm sure... (3 Replies)
Hello Experts,
I have a script that that transfers a file (via sftp) and it works fine but we ran into a snag where the target server asked for the ssh key and the script didn't know what to do. I want to add some logic to this script that at least sends an email that it didn't complete as... (4 Replies)
Hello,
We have to configure servers with a range of IPs which is in itself a subject for another script assistance request -but- we have run into quite a few IP ranges with routing problems lately.
I've been trying to figure out the best way to test a range of IPs, I mean, manually it's not... (4 Replies)
Hello All,
I was wondering if it would be possible to create a "racing" game in script. The game play would be as follows. Script will read the following input:
Start
|b| | | | |
|r| | | | |
First player (b) will roll a die to see how many spaces to move. This is will continue until the... (0 Replies)
I need to create a script that has a user enter a value. I want to verify that the value is either 1,2, or 3. If it is not then I want them to try entering it again. I am using a while loop to force them to retry.
I am able to test the input against 1,2, and 3, but when I test agains an... (4 Replies)
Is there a basic IP range script or a site that has basic script written?
I am making a script that is looking for a device on my network threw HTTP://. I and using a for loop with wget to download the page and using grep to search the code for the serial number.
My range is 172.16.x.x/16 I... (3 Replies)