[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2012-09-11
4
Medium Priority
?
713 Views
1 Endorsement
Last Modified: 2012-09-16
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
1
Comment
Question by:TIMFOX123
  • 2
4 Comments
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 38388909
grep -qF 10.10.10.55 route-eth2 && echo 'route-eth2
address
netmask
gateway'
0
 
LVL 23

Expert Comment

by:savone
ID: 38389137
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
 
LVL 23

Expert Comment

by:savone
ID: 38389285
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
 

Author Closing Comment

by:TIMFOX123
ID: 38403332
ozo hit what I wanted.

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

thx
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Suggested Courses
Course of the Month18 days, 12 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question