PDA

View Full Version : Do ... Loop



felix001
August 3rd, 2008, 08:49 PM
ill be the first to admit i am a bit of a bash scripting noob... but ive come againest a small problem...

LOG=/var/log/local2.log

for i in SEC-0 SEC-1 SEC-2 SEC-3 SEC-4 SEC-5 SEC-6 SEC-7
do
grep $i $LOG | wc -l
done

The above works and gives me 8 different values. Later on in the script I wish to echo these strings, so i would like them as varibles.
Is there anyway to assign each total against a varible so when this is finished I have... something like..
$SEC-0=some number
$SEC-1=some number ..... etc etc

I hope I have made this clear, thanks in advanced....

Felix .. http://fir3net.com

dexter.deepak
August 3rd, 2008, 09:27 PM
even i am a noob to this, but this should work :


LOG=/var/log/local2.log

for i in SEC-0 SEC-1 SEC-2 SEC-3 SEC-4 SEC-5 SEC-6 SEC-7
do
j=`grep $i $LOG | wc -l`
echo "$i=$j"
done

felix001
August 3rd, 2008, 10:17 PM
Its kinda right but i think i didnt explain what im trying to do.

Once outside of this loop I want to echo the variables (totals)..

This is the full code..


#!/bin/bash
#

# variables
LOG=/var/log/local2.log

for i in SEC-0 SEC-1 SEC-2 SEC-3 SEC-4 SEC-5 SEC-6 SEC-7
do
grep $i $LOG | wc -l
done

#### Functions

overview(){
echo "Emergency - Severity 0 - Logged $SEC-0"
echo "Alert - Severity 1 - Logged $SEC-1"
echo "Critical - Severity 2 - Logged $SEC-2"
echo "Error - Severity 3 - Logged $SEC-3"
echo "Warning - Severity 4 - Logged $SEC-4"
echo "Notification - Severity 5 - Logged $SEC-5"
echo "Informational - Severity 6 - Logged $SEC-6"
echo "Debugging - Severity 7 - Logged $SEC-7"
}

#### Main

echo "Cisco Log Viewer"
echo

if [ -f $LOG ] ; then
echo overview
else
echo "Unable to find logfile at $LOG" >&2
exit 1
fi

exit 0

Diabolis
August 3rd, 2008, 11:48 PM
What you want is an array to store each value inside your loop and when its done. Go through the array with another loop that will print its values.

ghostdog74
August 4th, 2008, 01:42 AM
you can use arrays. See here (http://tldp.org/LDP/abs/html/arrays.html)for examples. also read the whole bash manual in my sig. Also, for this


grep $i $LOG | wc -l

you can look at the grep man page for an option to just count the number of entries found. there's no need for wc -l

felix001
August 4th, 2008, 10:41 PM
ok ive been looking up arrays, and i have to say ... im completey confused...

i suppose the more ill learn so enough i should be able to find a fix to this issue soon enough...

dwhitney67
August 5th, 2008, 02:44 AM
Nothing to be confused about... it's quite easy. Here's an example:

#!/bin/sh

LOG=/var/log/local2.log

for i in 0 1 2 3 4 5 6 7
do
SEC_count[$i]=`grep SEC-$i $LOG | wc -l`
done

for i in 0 1 2 3 4 5 6 7
do
echo SEC-$i count = ${SEC_count[$i]}
done

felix001
August 5th, 2008, 08:26 PM
Thanks exactly what i was looking for.

Good stuff..

Thanks again...

Felix http://fir3net.com

felix001
August 5th, 2008, 11:26 PM
LOG="/var/log/local
level=(Emergency Alert Critical Error Warning Notification Informational Debugging)
for i in 0 1 2 3 4 5 6 7
do
printf "${level[$i]} - " ; grep -c SEC-$i $LOG
done



But the only thing im unsure of is how to allow for the wildcard in for the $LOG variable ????

ghostdog74
August 6th, 2008, 01:34 AM
what do you mean? explain more clearly.

felix001
August 6th, 2008, 08:28 PM
As in i have a variable LOG which i want to use for all my log files called local... so it would be

LOG=/var/log/local??????

ive tried the * and ? but they dont seem to work...

ghostdog74
August 7th, 2008, 01:32 AM
if they don't work, show how they don't work and any error messages.
Put set -x at the beginning of your script, run it and see what happens. Print out the value of LOG

dwhitney67
August 7th, 2008, 02:15 AM
The complaint appears to derive from LOG=/var/log/local2.log (as was listed in the original post). Apparently this is not satisfactory; the OP wants us to solve yet another problem.

OP... please give all of the requirements!!!!