Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
12-01-2009, 02:39 AM
|
#1
|
LQ Newbie
Registered: Dec 2009
Location: Michigan
Distribution: Fedora 11
Posts: 4
Rep:
|
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.
Here is the google doc link:
http://docs.google.com/Doc?docid=0AT...cXNid2dk&hl=en
Any helpful advice/fixes would be most appreciated.
|
|
|
12-01-2009, 02:56 AM
|
#2
|
Member
Registered: Sep 2009
Location: Russia
Distribution: Gentoo, LFS
Posts: 399
Rep:
|
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)
|
|
|
12-01-2009, 03:18 AM
|
#3
|
LQ Addict
Registered: Nov 2008
Location: Paris, France
Distribution: Slint64-15.0
Posts: 11,302
Rep: 
|
@ http://tldp.org you will find all what you need.
Suggested readings:
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
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
|
|
|
12-01-2009, 03:19 AM
|
#4
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
You might as well play around with the $RANDOM variable in bash.
... and read: http://www.tldp.org/LDP/abs/html/
-- edit --
in bash, to make the generated output limited to a range of numbers 0 to N - 1 , do something like this:
Code:
NUMBERTOGUESS=$(( RANDOM % N ))
# or
(( NUMBERTOGUESS = RANDOM % N ))
most of the time, 'man bash' should suit everything you need
Last edited by konsolebox; 12-01-2009 at 03:25 AM.
|
|
|
12-01-2009, 04:32 AM
|
#5
|
LQ Newbie
Registered: Dec 2009
Location: Michigan
Distribution: Fedora 11
Posts: 4
Original Poster
Rep:
|
Thanks, i'll check those out.
Also, what can i use to keep a count of the amount of guesses?
|
|
|
12-01-2009, 06:37 PM
|
#6
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,426
|
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.
Welcome to LQ

|
|
|
12-07-2009, 04:00 AM
|
#7
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
Quote:
Originally Posted by chrism01
A variable.... bash (Unix shells in general) are just a procedural prog lang; nothing fancy, but it does have access to any installed program.
|
Not just that. Bash can do more things: http://sf.net/projects/playshell
You can also *hack* its not-so-obvious capabilities to make it more powerful.. less procedural and more modular  .
|
|
|
12-07-2009, 04:41 AM
|
#8
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by trace_effect
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
|
|
|
12-10-2009, 01:54 AM
|
#9
|
Senior Member
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
|
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 02:03 AM.
|
|
|
12-11-2009, 02:23 AM
|
#10
|
LQ Newbie
Registered: Dec 2009
Location: Michigan
Distribution: Fedora 11
Posts: 4
Original Poster
Rep:
|
Very helpful posts i must say. I'm enjoying learning it.
|
|
|
All times are GMT -5. The time now is 09: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.
|
Latest Threads
LQ News
|
|