- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: while read line iteration cause variable value...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2010 07:21 AM
04-14-2010 07:21 AM
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
Solved! Go to Solution.
- Tags:
- while loop
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2010 08:28 AM
04-14-2010 08:28 AM
Re: while read line iteration cause variable value reset?
> 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2010 09:03 AM
04-14-2010 09:03 AM
Re: while read line iteration cause variable value reset?
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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2010 09:13 AM
04-14-2010 09:13 AM
Re: while read line iteration cause variable value reset?
OK, I see. See Duncan's comments here:
http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1265374
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2010 09:39 AM
04-14-2010 09:39 AM
SolutionI 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...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2010 10:55 AM
04-14-2010 10:55 AM
Re: while read line iteration cause variable value reset?
Thanks so much.
Pat
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2010 10:55 AM
04-14-2010 10:55 AM