1752781 Members
6259 Online
108789 Solutions
New Discussion юеВ

Re: loop in script

 
SOLVED
Go to solution
Shahril M
Frequent Advisor

loop in script

Hi,

Appreciate help what's wrong w/ the set syntax:

set -A MONFILE "/tmp/RC1.log" "/tmp/RC2.log"
count=0
for i in $MONFILE
do
ERR=`tail -1 $i|grep ^ERR|wc -l`
count=`expr $ERR + $count`
echo $i
done


Rgds,
Shahril

 

 

P.S. This thread has been moved from HP-UX > General to HP-UX > languages - HP Forums Moderator

10 REPLIES 10
Stefan Farrelly
Honored Contributor

Re: loop in script

your set -A doesnt work. Change your script to;

count=0
for i in "/tmp/RC1.log" "/tmp/RC2.log"
...

and expr doesnt work if not ^ERR found so change it to;
let count=$count+$ERR

and it works fine.
Im from Palmerston North, New Zealand, but somehow ended up in London...
Justo Exposito
Esteemed Contributor

Re: loop in script

Hi Shahril,

Try with this:
set -A MONFILE "/tmp/RC1.log" "/tmp/RC2.log"
count=0
for i in $MONFILE
do
ERR=`tail -1 $i|grep "^ERR" |wc -l`
if [ $ERR -gt 0 ]
then
(( count = $ERR + $count ))
fi
echo $i
done

Hope this help you,

Justo.
Help is a Beatiful word
Ralph Grothe
Honored Contributor

Re: loop in script

Sharil,

you reference your array incorrectly,
it should be like this

for file in ${MONFILE[*]}; do
# do something with $file
done
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: loop in script

Addendum,
if you really insist on indexing (which is kind of daft) you could do something like this (remember ${#MONFILE} gives the dimsion of your array)

typeset -i i=0
while [ i -lt ${#MONFILE[*] ]; do
file=${MONFILE[i]}
((i+=1))
done
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: loop in script

Oops typo,

the dimension of the array is of course
${#MONFILE[*]}

whereas

${#MONFILE}

only gives the length of the string of the 1st element
(but I guess you realized my typo ;-)
Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: loop in script

Ouch another typo crept in,
in the test condition of the while loop you have to prepend a dollar sign to the integer variable i,
(e.g. while [ $i -lt ...)
or use the [[ ]] test operator syntax
Madness, thy name is system administration
Steve Steel
Honored Contributor

Re: loop in script

Hi

If you just want to count the number of error lines in 2 files

typeset -i count=$(grep ^ERR /tmp/RC1.log /tmp/RC2.log)


Steve Steel
If you want truly to understand something, try to change it. (Kurt Lewin)
Jordan Bean
Honored Contributor

Re: loop in script

I think everyone answered this already to some good degree, but I'll add my 8 points worth.

set -A MONFILE "/tmp/RC1.log" "/tmp/RC2.log"
typeset -i count=0 ERR=0
for i in ${MONFILE[@]}
do
ERR=$(tail -1 $i | grep ^ERR | wc -l)
let count+=$ERR
echo $i
done
echo $count

To use all the elements of the array, the array name must be suffixed with [@] or [*] and wrapped in {}.


You could even do it this way:

typeset -i count=0
set -A MONFILE "/tmp/RC1.log" "/tmp/RC2.log"
for i in ${MONFILE[@]}
do
tail -1 $i
done | grep ^ERR | wc -l | read count
echo $count

David Totsch
Valued Contributor
Solution

Re: loop in script

Shahril:

I have to agree that using an array is more trouble than it is worth in this case. I would use filename generation:

count=0
for I in /tmp/RC?.log
do
ERR=$(tail -1 $I | grep -c ^ERR)
((count += ERR))
print $I
done

The code following "in" should interpret to a list. /tmp/RC?.log returns all files that match (e.g. RC1.log, RCa.log, RCA.log). If you want to restrict to numbers, use /tmp/RC[0-9].log

-dlt-