Re: loop in script (32 Views)
Reply
Frequent Advisor
Shahril M
Posts: 92
Registered: ‎11-20-2001
Message 1 of 11 (32 Views)
Accepted Solution

loop in script

[ Edited ]

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

Please use plain text.
Honored Contributor
Stefan Farrelly
Posts: 3,910
Registered: ‎10-23-1996
Message 2 of 11 (32 Views)

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...
Please use plain text.
Esteemed Contributor
Justo Exposito
Posts: 695
Registered: ‎10-14-2001
Message 3 of 11 (32 Views)

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
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 4 of 11 (32 Views)

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
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 5 of 11 (32 Views)

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
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 6 of 11 (32 Views)

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
Please use plain text.
Honored Contributor
Ralph Grothe
Posts: 2,631
Registered: ‎04-09-2000
Message 7 of 11 (32 Views)

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
Please use plain text.
Honored Contributor
Steve Steel
Posts: 2,909
Registered: ‎02-13-2000
Message 8 of 11 (32 Views)

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)
Please use plain text.
Honored Contributor
Jordan Bean
Posts: 579
Registered: ‎09-14-2000
Message 9 of 11 (32 Views)

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

Please use plain text.
Valued Contributor
David Totsch
Posts: 44
Registered: ‎02-07-1999
Message 10 of 11 (32 Views)

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-
Please use plain text.
Frequent Advisor
Shahril M
Posts: 92
Registered: ‎11-20-2001
Message 11 of 11 (32 Views)

Re: loop in script

I now learn that "set -A" constitutes to defining an array,
but under what circumstances is it necessary to subscript it to reference the elements?
I ask because I have seen instances of "for i in $HOST" and the like.

I used "set -A" because I need to reference the filenames again later in the script.

Thanx to those who have helped and taught me.


Rgds,
Shahril
Please use plain text.
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation