Operating System - HP-UX
1828245 Members
2651 Online
109975 Solutions
New Discussion

while read line iteration cause variable value reset?

 
SOLVED
Go to solution
RoyP
New Member

while read line iteration cause variable value reset?

Hi There.

First, I apologize. Used to be on HP-UX, but now Linux, but this is the best forum I've known for this sort of thing...

I've got a small script that basically checks if a process from a given process list is using more then a given MAXCPU utilization... the case being an email should be sent out. This is what I have. It seems like the $MailIt variable is reset on each iteration. As if the pipe / readline cause a new subshell to spawn? If that were the case, I already tried doing an export of the variable at first. Didn't help...

#!/bin/bash

MAXCPU=0
MailIt=0

(ps aux | grep f60webmx) | \
while read line
do
cpu=`echo $line | awk '{print $3}'`

#if [ ${cpu} -gt ${MAXCPU} ]; then
if awk 'BEGIN{if(0+'$cpu'>'$MAXCPU'+0)exit 0;exit 1}'; then
echo "GOT A HIT!!!"
MailIt=1
else
echo "no hit..."
fi
done

echo "MailIt = $MailIt"

Exit


The output I get is something like:

no hit...
no hit...
GOT A HIT!!!
no hit...
no hit...
GOT A HIT!!!
GOT A HIT!!!
no hit...
no hit...
no hit...
MailIt = 0


Any ideas??

Mny thanks. Pat
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor

Re: while read line iteration cause variable value reset?

Hi Pat:

> It seems like the $MailIt variable is reset on each iteration.

You never reset 'Mailit' to zero. Once set to one it says as one.

Regards!

...JRF...
RoyP
New Member

Re: while read line iteration cause variable value reset?

Hi James.

That's exactly my troubles: I DON'T reset MailIt to 0. But why is it that at the end, it comes out as 0 ?

I wish I had an hpux box to test this on... I don't It works as expected on Tru64, but not on Linux.

Can someone try the same little script (replacing the process Im looking for) on hpux?

I simply don't get it...
James R. Ferguson
Acclaimed Contributor

Re: while read line iteration cause variable value reset?

Hi (again) Pat:

OK, I see. See Duncan's comments here:

http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1265374

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor
Solution

Re: while read line iteration cause variable value reset?

Hi Pat:

I don't run the bash shell on my HP-UX servers, but I did look at this further in a LINUX instance. Consider:

#!/bin/bash
echo "pass-1"
V=0
cat /etc/hosts|while read LINE
do
(( V=V+1 ))
echo "${V} = ${LINE}"
done
echo ${V}

echo "pass-2"
V=0
while read LINE
do
(( V=V+1 ))
echo "${V} = ${LINE}"
done < /etc/hosts
echo ${V}
exit

...Thus, to rectify your problem you could do:

# cat ./newway
!/bin/bash
MYFILE=/tmp/stuff
trap 'rm ${MYFILE}' EXIT
MAXCPU=0
MailIt=0
ps aux | grep root > ${MYFILE}
while read line
do
cpu=`echo $line | awk '{print $3}'`

#if [ ${cpu} -gt ${MAXCPU} ]; then
if awk 'BEGIN{if(0+'$cpu'>'$MAXCPU'+0)exit 0;exit 1}'; then
echo "GOT A HIT!!!"
MailIt=1
else
echo "no hit..."
fi
done < ${MYFILE}
echo "MailIt = $MailIt"
exit

Regards!

...JRF...
RoyP
New Member

Re: while read line iteration cause variable value reset?

James, of course! Just put it all in a tmp file, removing the need of a pipe! I had thought about it, but did not know how to have the while loop process it without the use of pipe. I didn't know about the "done < /foo.txt" part!

Thanks so much.

Pat
RoyP
New Member

Re: while read line iteration cause variable value reset?

Closed as per James's solution.