Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| 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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
05-19-2010, 08:30 AM
|
#1
|
|
LQ Newbie
Registered: Aug 2009
Posts: 6
Rep:
|
How do i make a script that checks that my input is letters or numbers ?
Hi guys. as mentioned in the headline:
lets say. i would like to know how to make a script that can check that i input 3 letters and 3 numbers in some field.
i am completely noob in this programming scene, but think its quite interesting.
Hope rou gyus can help me.
Thanks in advance.
//Chris
|
|
|
|
05-19-2010, 08:37 AM
|
#2
|
|
Member
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381
Rep:
|
What have you tried so far ?
Tried Google ?
|
|
|
|
05-19-2010, 08:41 AM
|
#3
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,305
|
Well you could use straight bash or a command like grep, sed or awk and even other languages such as Perl.
So I would suggest either man of one of the commands or searching google for tutorials on a language.
|
|
|
|
05-20-2010, 04:01 AM
|
#4
|
|
LQ Newbie
Registered: Aug 2009
Posts: 6
Original Poster
Rep:
|
Hi again.
See below. this is what my script looks like.
Thanks in advance
#!/bin/bash
beertypes=(carlsberg tuborg masterbrew corona)
beertypenames=(car tub mas cor)
beertype=""
findBeerType()
{
num=0
for types in ${beertypenames[@]}; do
if [ $types = ${newbeer:0:3} ]; then
beertype=${beertypes[$num]}
else
num=$(( $num + 1 ))
fi
done
}
checkBeerName()
{
if [ ${#newbeer} -eq 6 ]; then
echo "hej"
if [ $newbeer -eq "[a-z][a-z][a-z][0-9][0-9][0-9]" ] ; then
echo "wowwow"
fi
fi
}
done
findBeerType
checkBeerName
echo $beertype
|
|
|
|
05-20-2010, 04:14 AM
|
#5
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Have a look at bash's [[ ]] test and its =~ regular expression comparison operator. You want a regular expression beginning with ^ and ending with $ (and the regular expression must not be quoted)..
|
|
|
|
05-20-2010, 04:18 AM
|
#6
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,305
|
Right, well firstly CODE tags are your friend, and ours so we can read your script better:
Code:
#!/bin/bash
beertypes=(carlsberg tuborg masterbrew corona)
beertypenames=(car tub mas cor)
beertype=""
findBeerType()
{
num=0
for types in ${beertypenames[@]}; do
if [ $types = ${newbeer:0:3} ]; then
beertype=${beertypes[$num]}
else
num=$(( $num + 1 ))
fi
done
}
checkBeerName()
{
if [ ${#newbeer} -eq 6 ]; then
echo "hej"
if [ $newbeer -eq "[a-z][a-z][a-z][0-9][0-9][0-9]" ] ; then
echo "wowwow"
fi
fi
}
done
findBeerType
checkBeerName
echo $beertype
So here are my questions for you:
1. As you are using functions, after you set the 3 variables at the beginning the first command is - done - Please explain??
2. You call the findBeerType function first but then refer to an uninitialised variable - newbeer - How is this supposed to work?
3. checkBeerName also refers to "newbeer" and again I cannot see where it receives values from??
Maybe if you answer some of these we can look at your issue, which I assume is the following:
Quote:
|
if [ $newbeer -eq "[a-z][a-z][a-z][0-9][0-9][0-9]" ] ; then
|
|
|
|
|
05-20-2010, 04:43 AM
|
#7
|
|
LQ Newbie
Registered: Aug 2009
Posts: 6
Original Poster
Rep:
|
Hi again - i thought "done" should be there to tell, that the first part i done  .
i dont know what you mean by uninitialized variable - this is almost the 1 time i tried this scripting .
i cant see it either where checkBeerName gets its value from , This is just how i thought it should be. Im sorry - i'm not skilled at programming  .
i think You are right
this is what i need to check for
if [ $newbeer -eq "[a-z][a-z][a-z][0-9][0-9][0-9]" ] ; then
i need to check , that there are 3 letters and 3 numeric values afterwards.
isnt this the way to do it ?.
Thanks.
I really appreciate your help !!
|
|
|
|
05-20-2010, 05:12 AM
|
#8
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,305
|
Quote:
|
Im sorry - i'm not skilled at programming
|
This is not a problem, we all start somewhere
You need to address the issues I have outlined before moving onto the correct way to do the last part.
I would suggest breaking your code down and making sure each function does what it is required to do before moving on.
Code:
findBeerType()
{
num=0
for types in ${beertypenames[@]}; do
if [ $types = ${newbeer:0:3} ]; then
beertype=${beertypes[$num]}
else
num=$(( $num + 1 ))
fi
done
}
In this function you have 6 variables:
num - counter initialised to 0
beertypenames - array initialised at the start of the script
types - set to each value in turn in the previous array
newbeer - ???
beertypes - array initialised at the start of the script
beertype - set to one of the beertypes based on comparison between types and newbeer (again this test cannot work as newbeer never set)
Try commenting out the rest of the code and get this function to work correctly first.
|
|
|
|
05-20-2010, 05:15 AM
|
#9
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by ministeren
this is what i need to check for
if [ $newbeer -eq "[a-z][a-z][a-z][0-9][0-9][0-9]" ] ; then
i need to check , that there are 3 letters and 3 numeric values afterwards.
isnt this the way to do it ?
|
It isn't for the following reasons: - -eq is a numeric comparison operator; you want the string comparison operator = (or == if you prefer; they are equivalent).
- The double quotes prevent bash expanding what's between them so bash compares $newbeer with exactly what is between the quotes.
- The [ ] form of testing does not expand [a-z][a-z][a-z][0-9][0-9][0-9] even without the double quotes. The [[ ]] form of testing is preferred for reasons explained here, which include doing what you want.
Putting that all together, you need if [[ $newbeer = [a-z][a-z][a-z][0-9][0-9][0-9] ]] ; then. This will work, as long as only lowercase letters are used and the letters appear before the numbers -- a refinement of your initial requirement of three letters and three numbers.
|
|
|
|
05-20-2010, 05:23 AM
|
#10
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by grail
Code:
beertype=${beertypes[$num]}
num=$(( $num + 1 ))
|
Hello Grail
Minor refinement, ever in search of the perfect code: you don't need the $ in $num in those places. It's not wrong but it's redundant. In both those places, bash expects a numerical expression and variables can be referenced in numerical expressions without a leading $.
Personallly I prefer let num++ over num=$(( $num + 1 )), finding it more legible but that's a matter of personal taste.
|
|
|
|
05-20-2010, 05:34 AM
|
#11
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,305
|
Hi catkin
I didn't put any of them in
I actually just go with ((num++))
|
|
|
|
05-20-2010, 06:35 AM
|
#12
|
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian Squeeze (server), Slackware 13.37 (netbook), Slackware64 14.0 (desktop),
Posts: 8,357
|
Quote:
Originally Posted by grail
Hi catkin
I didn't put any of them in
I actually just go with ((num++))
|
Ah! You are ahead of me already 
|
|
|
|
05-20-2010, 07:34 AM
|
#13
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,305
|
nah ... i still read all your posts when i see them 
|
|
|
|
05-20-2010, 09:36 AM
|
#14
|
|
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,006
Rep: 
|
I personally go with (( ++num )).. something I picked up from too much C++.
Anyhow, catkin's right on the mark. However, I'd use the [[:lower:]] and [[:digit:]] character classes instead of [a-z] and [0-9]. The symbolic character classes respect locale. The manual ones don't, with some pretty spectacular results sometimes.
Also, make sure to quote your array loop, like
Code:
for types in "${beertypenames[@]}" ; do
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:58 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
|
|