LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 08-10-2007, 04:30 AM   #1
jbraner
LQ Newbie
 
Registered: Jun 2007
Posts: 4

Rep: Reputation: 0
bash script question


Hi,

Please bear with me as this is all pretty new to me. I'm trying to write a (test) script where a user is prompted for input that must be a number. If it's not a number, I want to keep telling them to try again, until they get it right ;-)

For this reason, I tried to evaluate the input (called var1) in a while or until loop - but I have had no luck. The only way I can get regular expressions to work is by echoing var1 and then piping this to grep. Here is what I've got so far.

<snip>
echo "Type a whole number: "
read var1
# make sure we're getting a number as input
until test `echo $var1 | egrep -o [0-9]+`
do
echo "You have to type a number. Try again..."
read var1
done
# now strip out the non-numbers if they're there
var1=`echo $var1 | egrep -o [0-9]+`
</snip>
(I'm just taking this number and adding it to another number and printing the result on the screen - nothing earth shattering)

This part of the script will reject any input that does not contain any numbers, but it will still accept things like "55r" - so I used the "-o" switch for egrep to drop the "r" in this case.

What I would really like to do is reject *everything* that is not a number and make you "try again.." ;-)

Is there a regular expression for [0-9]+ but not letters (or other characters)? I tried things like [0-9^a-Z] but have had no luck.

Am I just getting the syntax wrong? Or do I have to nest a bunch of loops?
All the examples I can find for this sort of thing use "if .. then" statements, but this only works one time and will not keep making the user "try again"

I hope this makes sense.

John B
 
Old 08-10-2007, 04:47 AM   #2
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Like this?
Code:
#!/bin/bash

while true ; do
        echo "Enter a number: "
        read var
        if echo $var | egrep -q '^[0-9]+$' ; then
                break
        fi
        echo "'$var' is not a number. Try again..."
done
echo "Good"
 
Old 08-10-2007, 05:37 AM   #3
jbraner
LQ Newbie
 
Registered: Jun 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Rupert,

Thank you! That works perfectly!
If you don't mind - can i just ask one question?
egrep -q '^[0-9]+$'
this is returning the correct reply of only numbers
the regular expression reads (in English):
^[0-9] "the first character must be 0-9"
+ "matches one or more occurrrences after the initial one"
$ what does the $ do? I haven't seen this.


BTW I changed

if echo $var | egrep -q '^[0-9]+$' ; then

to

if `echo $var | egrep -q '^[0-9]+$'`
then
(added back tic and put then on seperate line)

Thanks again
 
Old 08-10-2007, 05:53 AM   #4
jbraner
LQ Newbie
 
Registered: Jun 2007
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jbraner
$ what does the $ do? I haven't seen this.
sorry - I was being thick. The $ means that it must *end* with this characrter too.

So it must start with a number, have any number of numbers and end with a number.
I *knew* there was a way to do this - thanks again
 
Old 08-10-2007, 07:34 AM   #5
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by jbraner
sorry - I was being thick. The $ means that it must *end* with this characrter too.

So it must start with a number, have any number of numbers and end with a number.
I *knew* there was a way to do this - thanks again
In the interest of learning, let's split some hairs.....
"^" means "the empty space at the beginning of the line"
"$" means "the empty space at the end of the line"

SO:
"[0-9]$" means "any numeral, immediately followed by the empty space a the end of the line"
 
Old 08-10-2007, 07:45 AM   #6
jbraner
LQ Newbie
 
Registered: Jun 2007
Posts: 4

Original Poster
Rep: Reputation: 0
pixellany,

All hair splitting is welcome. I have not seen it written this way before, but what you say makes perfect sense.

<still a *lot* more to learn>
 
  


Reply



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script question.... mkhan919 Linux - Newbie 3 04-06-2007 06:13 AM
bash script question edward_scott Linux - Newbie 4 08-26-2006 02:54 PM
bash script question xscousr Programming 5 07-03-2003 05:04 PM
Bash script question J_Szucs Linux - General 4 05-29-2003 08:48 AM
A bash script question J_Szucs Programming 2 05-13-2003 02:13 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 07:31 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration