Linux - GeneralThis 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
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.
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"
#!/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"
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)
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"
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.