The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Execute a shell script using regular expressions supergirl3954 UNIX for Dummies Questions & Answers 1 02-28-2008 07:20 AM
regular expressions ragha81 UNIX for Dummies Questions & Answers 2 03-05-2007 03:24 PM
Help with regular expressions arushunter Shell Programming and Scripting 13 12-23-2006 08:31 PM
using regular expressions in c shell control structure ballazrus Shell Programming and Scripting 3 02-19-2006 08:59 PM
Regular expressions in sed mfreemantle UNIX for Dummies Questions & Answers 3 02-11-2002 05:34 AM

Closed Thread
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 09-04-2001
Registered User
 

Join Date: Jun 2001
Posts: 35
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Regular Expressions in shell scripts <yawn>

Quite possibly a simple problem who's answer is evading me:

I am trying to write a sh shell script, part of which is *logically* trying to do the following:

if [ $1 = [A-Za-z] ]; then
...
fi

if [ $1 = [0-9.] ]; then
...
else
...
fi

Where the 1st condition is looking for a hostname passed as $1, the second a dotted-quad ip address, and the 3rd a catch-all error condition.

I cannot work out how to use regex with the test ([) command. Would $1 always evaluate to "TRUE"? And therefore I would use something like:

if [ $1 -a regex ]; then
...
fi

or not?
BTW: I know the regex I have given in the example are not water-tight for their application ([0-9.] doesn't necessarily guarantee a dotted-quad by any means), this is a tool which is only going to be used by myself, and therefore isn't too much of a problem

If someone would like to supply the regex for a dotted quad I would be most greatful :-), but no sweat.

Thanks in advance.
Forum Sponsor
  #2 (permalink)  
Old 09-04-2001
Registered User
 

Join Date: Jun 2001
Posts: 35
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Dotted Quad regex

Is this the correct regex for a dotted-quad IP address?

[0-9]\{1,3\}\.\{1\}[0-9]\{1,3\}\.\{1\}[0-9]\{1,3\}\.\{1\}[0-9]\{1,3\}
  #3 (permalink)  
Old 09-04-2001
Perderabo's Avatar
Unix Daemon
 

Join Date: Aug 2001
Location: Washington DC Area
Posts: 8,240
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
In ksh you can use patterns...
Code:
if [[ $1 = +([0-9]).+([0-9]).+([0-9]).+([0-9]) ]] ; then
   echo $1 is an ip address
else
   echo $1 is not an ip address
fi
  #4 (permalink)  
Old 09-04-2001
flim flam flamma jamma
 

Join Date: May 2001
Location: Chicago IL, USA
Posts: 1,006
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
stright from Mastering Regular Expresstions by Oreilly page 124

Code:
^([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.
([01]?\d\d?|2[0-4]\d|25[0-5])\.([01]?\d\d?|2[0-4]\d|25[0-5])\.$
this should all be on 1 line btw. the reason 0-9 wont work as mentioned above it cuz if you want a ligit ip all the time 0-9 wont work. remember 0-9 says even an ip of 999.999.999.999 is valid when we all know is not valid in any universe (yet anyways).

btw this book is a book i think everyone that is into unix, programing, scripting should read.

the hostname check is on page 167

Last edited by Optimus_P; 09-04-2001 at 02:38 PM.
Google UNIX.COM
Closed Thread

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:36 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101