LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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!

Notices



Reply
 
Search this Thread
Old 04-12-2009, 06:39 AM   #1
Aveltium
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.

Code:
echo -n "Input: "
read x
if [ $x == ${...err i dont know what to put here} ]
then echo "Valid"
else echo "Invalid"
fi
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:
Code:
echo -n "Input: " ; read x
if [ $(echo "$x" | grep -c "^[0-9]\{4\}$") -eq 1 ] ; then
     echo "Valid"
else
     echo "Invalid"
fi
And this is what I have learned while I was trying to ask for this, on another forum: http://www.unix.com/302306293-post8.html
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-13-2009 at 12:12 AM.
 
Old 04-12-2009, 06:43 AM   #2
laceyj
LQ Newbie
 
Registered: Jun 2003
Posts: 5

Rep: Reputation: 1
Try looking here:
http://www.amk.ca/python/howto/regex/
 
Old 04-12-2009, 07:16 AM   #3
Aveltium
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, 09:05 AM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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, 09:39 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,049

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Quote:
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-13-2009, 12:14 AM   #6
Aveltium
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!
 
  


Reply

Tags
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 07:28 AM
pattern matching in file amitpardesi Linux - Software 5 02-08-2008 08:06 AM
Pattern Matching Problem ratul_11 Programming 3 12-28-2007 01:27 AM
pattern matching nadeemr Linux - Newbie 8 06-13-2007 12:05 PM
pattern matching in perl ludeKing Programming 9 04-02-2004 10:53 AM


All times are GMT -5. The time now is 08:01 AM.

Main Menu
Advertisement
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