LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   newbie help on small shell script (https://www.linuxquestions.org/questions/programming-9/newbie-help-on-small-shell-script-363563/)

ripcurl 09-15-2005 07:01 AM

newbie help on small shell script
 
Hey all,

I've got a fair few router configs backed up to a directory on one of my unix hosts.
They are backed up to /home/configs/<router name>/<router name>.cnfg.

What i'am hoping for is a script that can create a list of routers that have a certain line in their config. eg (all routers with 'e1' in their configs)

I was thinking the logical steps i need to take are
1. create list of directorys in /home/configs directory
2. somehow create script to "cat /<router name>/<router name>.cnfg | grep e1" and then IF e1 exists in the config copy the routers name to a file.
3. contine until all the configs are finished, appending routers it finds with "e1" in the config to the file.

But i've got no idea how to actually archive this
Can anyone suggest a script to do this, or a better way of doing this?

keefaz 09-15-2005 07:18 AM

Maybe try :
Code:

grep -rl e1 /home/configs | sed -e 's#.*/##' -e 's#.cnfg##' > list.txt

hhamid 09-15-2005 07:55 AM

I have written a good script for you;
I hope it is wath you want

Hamid Hajabdolali

------------------------------

#!/bin/bash

PATHTOSEARCH="/home/configs"
TEXT="YOUR_TEXT_TO_SEARCH"
OUTPUTFILE="Result.txt"

if [ -e $PATHTOSEARCH/$OUTPUTFILE ]; then
rm -f $PATHTOSEARCH/$OUTPUTFILE
fi

for DIR in $PATHTOSEARCH/*
do
#Checking for the .cnfg file in router folder
if [ -e $DIR/$DIR.cnfg ]; then
grep $TEXT $DIR/$DIR.cnfg > /dev/null
if [ $? == 0 ]; then
echo ${DIR:2} >> $PATHTOSEARCH/$OUTPUTFILE
fi
fi
done

ripcurl 09-15-2005 09:19 AM

Quote:

Originally posted by keefaz
Maybe try :
Code:

grep -rl e1 /home/configs | sed -e 's#.*/##' -e 's#.cnfg##' > list.txt

Thanks keefaz, that seems to work great in my Cywin testing.

I'll get back to you tomorrow and tell you how it goes.

ripcurl 09-16-2005 11:11 AM

Hey all,
I examined my records a bit more closely and the configs are actually recorded in txt files under the name of <router name>-confg

No file extensions, only the -confg at the end to indicate it's a router config.

How could i modify the script above to read these files?
"grep -rl e1 /home/configs | sed -e 's#.*/##' -e 's#.confg##' > list.txt"

archtoad6 09-20-2005 03:17 PM

Change the '/home/configs' to '/home/configs/*-confg'


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