New to linux and have a question about a shell script I'm attempting to write
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
New to linux and have a question about a shell script I'm attempting to write
Hello,
I am taking a beginning linux class as part of my BS and I had been advised to have a go at making a shell script. The example states:
Create a script that prompts a user to guess a number (that the script creator selects beforehand and enters in the script), if the user guesses the number they are notified and if not the program terminates.
So, i messed around a bit with my limited knowledge and a reference book and came up with a non-working script that im not sure how to fix.
well, main idea is to use /dev/urandom to generate a random number and then prompt for user input, then compare it with value.
i suggest you'd better read shell-scripting how-to's before you start. (in same google search)
and from this page:
- Introduction to Linux - A Hands on Guide from Machtel Garrels
- Bash Guide for Beginners from Machtel Garrels
- Advanced Bash-Scripting Guide from Mendel Cooper
A variable.... bash (Unix shells in general) are just a procedural prog lang; nothing fancy, but it does have access to any installed program.
Have a read of those guides; should be fairly straightforward.
Come back with your code if you get stuck.
Also, what can i use to keep a count of the amount of guesses?
That's outside the specification's "if the user guesses the number they are notified and if not the program terminates" but could be done by putting the guess read-and-test code in a loop with a counter
Code:
count=0
while true
do
<read guess>
<compare guess and terminate with message detailing $count if correct>
let count=count+1
done
following catkin's suggestion, although less simpler, in bash it can be done like:
Code:
# constants
GUESSRANGE=10
MAXGUESSES=3
# variables
GUESSED=false
NUMBERTOGUESS=$(( ( RANDOM % GUESSRANGE ) + 1 )) # () may be opt.
# enable extended globbing
shopt -s extglob # supported starting bash 3.0
# ask
echo "Please guess a number. The range is from 1 to ${GUESSRANGE}."
for (( I = 1; I <= MAXGUESSES; )); do
read -p "Your number ($(( MAXGUESSES - I + 1 )) tries left): " GUESS
if [[ "$GUESS" != +([[:digit:]]) ]]; then
echo "Reply was not a number."
elif [[ GUESS -lt 1 || GUESS -gt GUESSRANGE ]]; then
echo "Number was not in range."
elif [[ GUESS -ne NUMBERTOGUESS ]]; then
echo "Wrong guess. Please try again."
(( ++I ))
else
echo "Congratulations. You're correct."
GUESSED=true
break
fi
done
if [[ "$GUESSED" = true ]]; then
<do some things when user has successfully guessed the number.>
else
echo "Sorry. Maximum number of guess attempts was reached."
<do some things when user failed to guess the number.>
fi
-- edit --
The result was obviously a bad programming practice. I just tried to use 'for (( ;; ))' but we can find that it's better to use 'while :; do'.
Last edited by konsolebox; 12-10-2009 at 01:03 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.