Bash script to print name and contents of a file if the file contains $string

I am working on checking routes on many machines

I want to check my /etc/sysconfig/network-scripts/route-ethx  file for an ip contained in the file ( will will call it $string )

so if 10.10.10.55 is  in route-eth2

I want to print out:
 route-eth2
address
netmask
gateway


I thing a  find -exec would do it, a perl one liner or something easy

thx
TIMFOX123Asked:
Who is Participating?
 
ozoCommented:
grep -qF 10.10.10.55 route-eth2 && echo 'route-eth2
address
netmask
gateway'
0
 
savoneCommented:
I love a scripting challenge, it helps me learn (pretty much a beginner myself)...

So here is what I did... First of all I am working with a file called route-eth0 placed in my /etc/sysconfig/network-scripts/ directory and it has the following contents:

GATEWAY0=192.168.1.254
NETMASK0=255.255.255.0
ADDRESS0=192.168.55.0
GATEWAY1=10.164.234.112
NETMASK1= 255.255.255.240
ADDRESS1=10.164.234.132

Now I run my script:

[savona@bighat tmp]$ ./route.sh 192.168.1.254
/etc/sysconfig/network-scripts/route-eth0
GATEWAY0=192.168.1.254
NETMASK0=255.255.255.0
ADDRESS0=192.168.55.0

I think that output it what you wanted correct?   Here is the script I used:

#!/bin/bash
for i in `ls /etc/sysconfig/network-scripts/route*`; do
        if grep -q $1 $i; then
                NEEDED=`grep $1 $i`
                NUMBER=`echo ${NEEDED:7:1}`
                echo $i
                grep '[A-Z]'$NUMBER $i
        fi
done

Open in new window

0
 
savoneCommented:
BTW you have to pass the address your looking for to the script (if you did not notice that), for example lets say you wanted to search for 192.168.1.254 you would run the script like this:

./route.sh 192.168.1.254
0
 
TIMFOX123Author Commented:
ozo hit what I wanted.

There was a nice script written however it did not do quite what I wanted.  

thx
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.