LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-19-2010, 09:30 AM   #1
ministeren
LQ Newbie
 
Registered: Aug 2009
Posts: 6

Rep: Reputation: 0
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
 
Old 05-19-2010, 09:37 AM   #2
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
What have you tried so far ?
Tried Google ?
 
Old 05-19-2010, 09:41 AM   #3
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,627

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
Old 05-20-2010, 05:01 AM   #4
ministeren
LQ Newbie
 
Registered: Aug 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Thumbs down

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
 
Old 05-20-2010, 05:14 AM   #5
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,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
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)..
 
Old 05-20-2010, 05:18 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,627

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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
 
Old 05-20-2010, 05:43 AM   #7
ministeren
LQ Newbie
 
Registered: Aug 2009
Posts: 6

Original Poster
Rep: Reputation: 0
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 !!
 
Old 05-20-2010, 06:12 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,627

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
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.
 
Old 05-20-2010, 06:15 AM   #9
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,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Quote:
Originally Posted by ministeren View Post
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:
  1. -eq is a numeric comparison operator; you want the string comparison operator = (or == if you prefer; they are equivalent).
  2. The double quotes prevent bash expanding what's between them so bash compares $newbeer with exactly what is between the quotes.
  3. 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.
 
Old 05-20-2010, 06:23 AM   #10
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,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Quote:
Originally Posted by grail View Post
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.
 
Old 05-20-2010, 06:34 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,627

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
Hi catkin

I didn't put any of them in

I actually just go with ((num++))
 
Old 05-20-2010, 07:35 AM   #12
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,557
Blog Entries: 28

Rep: Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178Reputation: 1178
Quote:
Originally Posted by grail View Post
Hi catkin

I didn't put any of them in

I actually just go with ((num++))
Ah! You are ahead of me already
 
Old 05-20-2010, 08:34 AM   #13
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,627

Rep: Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947Reputation: 1947
nah ... i still read all your posts when i see them
 
Old 05-20-2010, 10:36 AM   #14
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,014

Rep: Reputation: 115Reputation: 115
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
 
  


Reply


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
laptop keyboard annoying numlock making letters become numbers linuxmandrake Linux - Hardware 5 10-13-2007 11:36 AM
LXer: Google mixes up letters and numbers LXer Syndicated Linux News 0 10-12-2006 12:54 PM
Numbers instead of letters on IBM TrackPoint Keyboard / FC3 kalico Linux - Hardware 0 09-12-2006 05:13 PM
How to make a script wait for input farmerjoe Linux - General 2 12-28-2004 06:18 AM
Make script wait for input farmerjoe Linux - General 4 12-28-2004 02:49 AM


All times are GMT -5. The time now is 09:21 PM.

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