regex inside if comparison


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting regex inside if comparison
# 1  
Old 09-03-2008
regex inside if comparison

I'm trying to compare the last octet of an IP to a regex:

Code:
IP=$(ifconfig eth0 | grep inet | awk -F: '{print $2}' | awk -F. '{print $4}' | awk '{print $1}')
 
            if [ $IP -eq ^1[1-3][0-9] ]; then
                echo "GOOD: Correct IP range for server"
            else
                echo "ERROR: Incorrect IP range for server"
            fi

It looks like it is literally comparing the regex.

'[' 121 -eq '^1[1-3][0-9]' ']'
./temp.sh: line 23: [: ^1[1-3][0-9]: integer expression expected
+ echo 'ERROR: Incorrect IP range for server'
ERROR: Incorrect IP range for server

Any ideas how to fix this?
# 2  
Old 09-03-2008
-eq is a numeric comparison, you want to do a string comparison:

Code:
if [[ $IP = 1[1-3][0-9] ]]; then

Note also that it is not a regular expression as such, [[ uses the simpler filename matching type syntax (as you would use for file specifications).

Note also that there's no need for so many awks and greps, this should be enough to pull out that last octet:

Code:
ifconfig eth0 | awk -F '[ :.]+' '/inet/ {print $7}'

# 3  
Old 09-03-2008
Although personally I would do the comparison numerically anyway:

Code:
if [[ "$IP" -ge 110 && "$IP" -le 139 ]]; then

# 4  
Old 09-03-2008
Quote:
Originally Posted by Annihilannic
-eq is a numeric comparison, you want to do a string comparison:

Code:
if [[ $IP = 1[1-3][0-9] ]]; then

Note also that it is not a regular expression as such, [[ uses the simpler filename matching type syntax (as you would use for file specifications).
Awesome, thanks for that and the other snippets. To be clear, why is this not a regex?
# 5  
Old 09-03-2008
Just because... it isn't. Smilie It wasn't implemented that way. You can use expr string : regex if you wish to match against a Basic Regular Expression.
# 6  
Old 09-03-2008
Quote:
Originally Posted by Annihilannic
Just because... it isn't. Smilie It wasn't implemented that way. You can use expr string : regex if you wish to match against a Basic Regular Expression.

Fair enough, thanks again.
# 7  
Old 09-04-2008
Don't forget case which does support a constrained form of regular expression (but shell wildcards are not real regular expressions).

Code:
case $IP in 1[1-3][0-9]*) echo good;; esac

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sendmail K command regex: adding exclusion/negative lookahead to regex -a@MATCH

I'm trying to get some exclusions into our sendmail regular expression for the K command. The following configuration & regex works: LOCAL_CONFIG # Kcheckaddress regex -a@MATCH +<@+?\.++?\.(us|info|to|br|bid|cn|ru) LOCAL_RULESETS SLocal_check_mail # check address against various regex... (0 Replies)
Discussion started by: RobbieTheK
0 Replies

2. Shell Programming and Scripting

Perl, RegEx - Help me to understand the regex!

I am not a big expert in regex and have just little understanding of that language. Could you help me to understand the regular Perl expression: ^(?!if\b|else\b|while\b|)(?:+?\s+){1,6}(+\s*)\(*\) *?(?:^*;?+){0,10}\{ ------ This is regex to select functions from a C/C++ source and defined in... (2 Replies)
Discussion started by: alex_5161
2 Replies

3. Shell Programming and Scripting

Renumber position 88-94 inside all files matching criteria inside folder

There are 4 files inside one folder matching criteria i.e. File name = ABCJmdmfbsjopXXXXXXX_mm-dd-yyyy_XXX.data Here is the Code which find the files matching criteria:- TS=`date +"%m-%d-%Y"`| for fname in `find . -name "ABCJmdmfbsjop???????_${TS}*.data"` do # Matching File Processing Code.... (1 Reply)
Discussion started by: lancesunny
1 Replies

4. UNIX for Dummies Questions & Answers

read regex from ID file, print regex and line below from source file

I have a file of protein sequences with headers (my source file). Based on a list of IDs (which are included in some of the headers), I'd like to print out only the specified sequences, with only the ID as header. In other words, I'd like to search source.txt for the terms in IDs.txt, and print... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

5. UNIX for Dummies Questions & Answers

How to specify beginning-of-line/end-of-line characters inside a regex range

How can I specify special meaning characters like ^ or $ inside a regex range. e.g Suppose I want to search for a string that either starts with '|' character or begins with start-of-line character. I tried the following but it does not work: sed 's/\(\)/<do something here>/g' file1 ... (3 Replies)
Discussion started by: jawsnnn
3 Replies

6. Shell Programming and Scripting

string comparison not working inside while loop

The string comparison highlighted below is not working fine. Please help: while read line do # Get File name by deleting everything that preceedes and follows Filename as printed in cvs status' output f_name=`echo $line | sed -e 's/^File://' -e 's/ *Status:.*//' | awk '{print $NF}'` ... (4 Replies)
Discussion started by: sudvishw
4 Replies

7. Shell Programming and Scripting

String comparison not working inside while loop

Hi, In the code included below, the string comparision is not working fine. Please help while (( find_out >= i )) do file=`head -$i f.out|tail -1` dir=`dirname $file` cd $dir Status="" if ; then Status=`cvs -Q status... (3 Replies)
Discussion started by: sudvishw
3 Replies

8. Shell Programming and Scripting

Converting perl regex to sed regex

I am having trouble parsing rpm filenames in a shell script.. I found a snippet of perl code that will perform the task but I really don't have time to rewrite the entire script in perl. I cannot for the life of me convert this code into something sed-friendly: if ($rpm =~ /(*)-(*)-(*)\.(.*)/)... (1 Reply)
Discussion started by: suntzu
1 Replies

9. Shell Programming and Scripting

Matching using Regex inside a file

I need scan through some files, then open the file one by one and scan inside the file using perl to see if it contain a start tag and end tag which the end tag is the mirror image of the start tag, the start tag and end tag only have 5 char. And inside the file there is "http://". It is just a... (5 Replies)
Discussion started by: blueblur
5 Replies

10. Shell Programming and Scripting

looping a array inside inside ssh is not working, pls help

set -A arr a1 a2 a3 a4 # START ssh -xq $Server1 -l $Username /usr/bin/ksh <<-EOS integer j=0 for loop in ${arr} do printf "array - ${arr}\n" (( j = j + 1 )) j=`expr j+1` done EOS # END ========= this is not giving me correct output. I... (5 Replies)
Discussion started by: reldb
5 Replies
Login or Register to Ask a Question