LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 12-01-2009, 01:39 AM   #1
trace_effect
LQ Newbie
 
Registered: Dec 2009
Location: Michigan
Distribution: Fedora 11
Posts: 4

Rep: Reputation: 0
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.
 
Old 12-01-2009, 01:56 AM   #2
Web31337
Member
 
Registered: Sep 2009
Location: Russia
Distribution: Gentoo, LFS
Posts: 399
Blog Entries: 71

Rep: Reputation: 65
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)
 
Old 12-01-2009, 02:18 AM   #3
Didier Spaier
Senior Member
 
Registered: Nov 2008
Location: Paris, France
Distribution: Slackware{,64}-{14.1,current} on a Lenovo Thinkpad T61 6457-4XG
Posts: 4,370

Rep: Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085Reputation: 1085
@ 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
 
Old 12-01-2009, 02:19 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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 02:25 AM.
 
Old 12-01-2009, 03:32 AM   #5
trace_effect
LQ Newbie
 
Registered: Dec 2009
Location: Michigan
Distribution: Fedora 11
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks, i'll check those out.

Also, what can i use to keep a count of the amount of guesses?
 
Old 12-01-2009, 05:37 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,287

Rep: Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034Reputation: 2034
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
 
Old 12-07-2009, 03:00 AM   #7
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by chrism01 View Post
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 .
 
Old 12-07-2009, 03:41 AM   #8
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by trace_effect View Post
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
 
Old 12-10-2009, 12:54 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
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.
 
Old 12-11-2009, 01:23 AM   #10
trace_effect
LQ Newbie
 
Registered: Dec 2009
Location: Michigan
Distribution: Fedora 11
Posts: 4

Original Poster
Rep: Reputation: 0
Very helpful posts i must say. I'm enjoying learning it.
 
  


Reply

Tags
shell scripting


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to ssh from a shell script ? For ppl who can write shell scripts. thefountainhead100 Programming 14 10-22-2008 06:24 AM
[SHELL SCRIPT] Write at the right of the shell window Creak Linux - General 2 04-02-2004 03:00 PM


All times are GMT -5. The time now is 08:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration