Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 04-12-2009, 05:39 AM   #1
LQ Newbie
Registered: Apr 2009
Posts: 5

Rep: Reputation: 0
Pattern Matching

Hi, I'm very new to shell scripting and I'm sorry if this question is too dumb.
If it is ok, please link me to some beginner guides for questions like this one.

I want to check if the entered string is a number and has 4 digits.

I heard that I should use "regular expression" or something like that to accomplish this task, but I don't know how to use it.

echo -n "Input: "
read x
if [ $x == ${...err i dont know what to put here} ]
then echo "Valid"
else echo "Invalid"
Any help is much appreciated!

* Edit:

I have found the answer for my question, hope it helps anyone who would like to search for this:
echo -n "Input: " ; read x
if [ $(echo "$x" | grep -c "^[0-9]\{4\}$") -eq 1 ] ; then
     echo "Valid"
     echo "Invalid"
And this is what I have learned while I was trying to ask for this, on another forum:
I want to share it because I think it's nice and helpful. Please don't hate me for linking to other forums.

Last edited by Aveltium; 04-12-2009 at 11:12 PM.
Old 04-12-2009, 05:43 AM   #2
LQ Newbie
Registered: Jun 2003
Posts: 5

Rep: Reputation: 1
Try looking here:
Old 04-12-2009, 06:16 AM   #3
LQ Newbie
Registered: Apr 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks for responding and the link!

I'm learning Bash shell script at the college. Is that Python the same or similar?
Old 04-12-2009, 08:05 AM   #4
Senior Member
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you are learning bash, then check out the bash link in my sig. Somewhere down the road while you are reading, there are examples of how to check for digits. go read it. good luck
Old 04-12-2009, 08:39 AM   #5
Gentoo support team
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,074

Rep: Reputation: 386Reputation: 386Reputation: 386Reputation: 386
Originally Posted by Aveltium View Post
I'm learning Bash shell script at the college. Is that Python the same or similar?
Bash and python have nothing to do with each other.

Bash is a shell. It's been made basically to let you enter commands. It's good when it comes to structuring batches of commands as well. And it has some advanced capabilities when compared to other simpler shells. But it's not a general purpose programming language. You can start to realize that when you need to do some advanced operation whit patterns and you have to resort to awk or sed, or when you need true arithmetic capabilities and you have to resort to bc, and so we could continue.

It lacks even data types, since for bash everything is a string. Bash is also damn slow.

Python on the contrary is a proper general purpose programming language, just like perl or C.
Old 04-12-2009, 11:14 PM   #6
LQ Newbie
Registered: Apr 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks a lot laceyj, ghostdog74 and i92guboj! That helps a lot for newbies like me!


beginner, expression, matching, pattern, regular

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
pattern matching help needed. squirrel001 Linux - Newbie 2 06-17-2008 06:28 AM
pattern matching in file amitpardesi Linux - Software 5 02-08-2008 07:06 AM
Pattern Matching Problem ratul_11 Programming 3 12-28-2007 12:27 AM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 11:05 AM
pattern matching in perl ludeKing Programming 9 04-02-2004 09:53 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:58 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration