BladeSystem Forums Have Moved here
To make BladeSystem information easier to find, we have moved the BladeSystem forums here, to Servers and Operating Systems.
Showing results for 
Search instead for 
Do you mean 

loop in script

SOLVED
Go to Solution
Frequent Advisor

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

10 REPLIES
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...
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
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
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
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
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
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)
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

Valued Contributor

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-
Highlighted
Frequent Advisor

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