How do i make a script that checks that my input is letters or numbers ?
ProgrammingThis 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.
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.
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
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)..
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
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.
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.