<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: while read line iteration cause variable value reset? in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235043#M675693</link>
    <description>Hi (again) Pat:&lt;BR /&gt;&lt;BR /&gt;OK, I see.  See Duncan's comments here:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1265374" target="_blank"&gt;http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1265374&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
    <pubDate>Wed, 14 Apr 2010 16:13:25 GMT</pubDate>
    <dc:creator>James R. Ferguson</dc:creator>
    <dc:date>2010-04-14T16:13:25Z</dc:date>
    <item>
      <title>while read line iteration cause variable value reset?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235040#M675690</link>
      <description>&lt;!--!*#--&gt;Hi There.&lt;BR /&gt;&lt;BR /&gt;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... &lt;BR /&gt;&lt;BR /&gt;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... &lt;BR /&gt;&lt;BR /&gt;#!/bin/bash &lt;BR /&gt;&lt;BR /&gt;MAXCPU=0 &lt;BR /&gt;MailIt=0 &lt;BR /&gt;&lt;BR /&gt;(ps aux | grep f60webmx) | \ &lt;BR /&gt;while read line &lt;BR /&gt;do &lt;BR /&gt;  cpu=`echo $line | awk '{print $3}'` &lt;BR /&gt;&lt;BR /&gt;  #if [ ${cpu} -gt ${MAXCPU} ]; then &lt;BR /&gt;  if awk 'BEGIN{if(0+'$cpu'&amp;gt;'$MAXCPU'+0)exit 0;exit 1}'; then &lt;BR /&gt;    echo "GOT A HIT!!!" &lt;BR /&gt;    MailIt=1 &lt;BR /&gt;  else &lt;BR /&gt;    echo "no hit..." &lt;BR /&gt;  fi &lt;BR /&gt;done &lt;BR /&gt;&lt;BR /&gt;echo "MailIt = $MailIt" &lt;BR /&gt;&lt;BR /&gt;Exit &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;The output I get is something like: &lt;BR /&gt;&lt;BR /&gt;no hit... &lt;BR /&gt;no hit... &lt;BR /&gt;GOT A HIT!!! &lt;BR /&gt;no hit... &lt;BR /&gt;no hit... &lt;BR /&gt;GOT A HIT!!! &lt;BR /&gt;GOT A HIT!!! &lt;BR /&gt;no hit... &lt;BR /&gt;no hit... &lt;BR /&gt;no hit... &lt;BR /&gt;MailIt = 0 &lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Any ideas??&lt;BR /&gt;&lt;BR /&gt;Mny thanks. Pat&lt;BR /&gt;</description>
      <pubDate>Wed, 14 Apr 2010 14:21:55 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235040#M675690</guid>
      <dc:creator>RoyP</dc:creator>
      <dc:date>2010-04-14T14:21:55Z</dc:date>
    </item>
    <item>
      <title>Re: while read line iteration cause variable value reset?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235041#M675691</link>
      <description>Hi Pat:&lt;BR /&gt;&lt;BR /&gt;&amp;gt; It seems like the $MailIt variable is reset on each iteration.&lt;BR /&gt;&lt;BR /&gt;You never reset 'Mailit' to zero.  Once set to one it says as one.&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 14 Apr 2010 15:28:03 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235041#M675691</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2010-04-14T15:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: while read line iteration cause variable value reset?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235042#M675692</link>
      <description>Hi James. &lt;BR /&gt;&lt;BR /&gt;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 ?&lt;BR /&gt;&lt;BR /&gt;I wish I had an hpux box to test this on... I don't It works as expected on Tru64, but not on Linux.&lt;BR /&gt;&lt;BR /&gt;Can someone try the same little script (replacing the process Im looking for) on hpux?&lt;BR /&gt;&lt;BR /&gt;I simply don't get it...&lt;BR /&gt;</description>
      <pubDate>Wed, 14 Apr 2010 16:03:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235042#M675692</guid>
      <dc:creator>RoyP</dc:creator>
      <dc:date>2010-04-14T16:03:40Z</dc:date>
    </item>
    <item>
      <title>Re: while read line iteration cause variable value reset?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235043#M675693</link>
      <description>Hi (again) Pat:&lt;BR /&gt;&lt;BR /&gt;OK, I see.  See Duncan's comments here:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1265374" target="_blank"&gt;http://forums.itrc.hp.com/service/forums/questionanswer.do?threadId=1265374&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 14 Apr 2010 16:13:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235043#M675693</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2010-04-14T16:13:25Z</dc:date>
    </item>
    <item>
      <title>Re: while read line iteration cause variable value reset?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235044#M675694</link>
      <description>&lt;!--!*#--&gt;Hi Pat:&lt;BR /&gt;&lt;BR /&gt;I don't run the bash shell on my HP-UX servers, but I did look at this further in a LINUX instance.  Consider:&lt;BR /&gt;&lt;BR /&gt;#!/bin/bash&lt;BR /&gt;echo "pass-1"&lt;BR /&gt;V=0&lt;BR /&gt;cat /etc/hosts|while read LINE&lt;BR /&gt;do&lt;BR /&gt;    (( V=V+1 ))&lt;BR /&gt;    echo "${V} = ${LINE}"&lt;BR /&gt;done&lt;BR /&gt;echo ${V}&lt;BR /&gt;&lt;BR /&gt;echo "pass-2"&lt;BR /&gt;V=0&lt;BR /&gt;while read LINE&lt;BR /&gt;do&lt;BR /&gt;    (( V=V+1 ))&lt;BR /&gt;    echo "${V} = ${LINE}"&lt;BR /&gt;done &amp;lt; /etc/hosts&lt;BR /&gt;echo ${V}&lt;BR /&gt;exit&lt;BR /&gt;&lt;BR /&gt;...Thus, to rectify your problem you could do:&lt;BR /&gt;&lt;BR /&gt;# cat ./newway&lt;BR /&gt;!/bin/bash&lt;BR /&gt;MYFILE=/tmp/stuff&lt;BR /&gt;trap 'rm ${MYFILE}' EXIT&lt;BR /&gt;MAXCPU=0&lt;BR /&gt;MailIt=0&lt;BR /&gt;ps aux | grep root     &amp;gt; ${MYFILE}&lt;BR /&gt;while read line&lt;BR /&gt;do&lt;BR /&gt;  cpu=`echo $line | awk '{print $3}'`&lt;BR /&gt;&lt;BR /&gt;  #if [ ${cpu} -gt ${MAXCPU} ]; then&lt;BR /&gt;  if awk 'BEGIN{if(0+'$cpu'&amp;gt;'$MAXCPU'+0)exit 0;exit 1}'; then&lt;BR /&gt;    echo "GOT A HIT!!!"&lt;BR /&gt;    MailIt=1&lt;BR /&gt;  else&lt;BR /&gt;    echo "no hit..."&lt;BR /&gt;  fi&lt;BR /&gt;done &amp;lt; ${MYFILE}&lt;BR /&gt;echo "MailIt = $MailIt" &lt;BR /&gt;exit&lt;BR /&gt;&lt;BR /&gt;Regards!&lt;BR /&gt;&lt;BR /&gt;...JRF...</description>
      <pubDate>Wed, 14 Apr 2010 16:39:25 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235044#M675694</guid>
      <dc:creator>James R. Ferguson</dc:creator>
      <dc:date>2010-04-14T16:39:25Z</dc:date>
    </item>
    <item>
      <title>Re: while read line iteration cause variable value reset?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235045#M675695</link>
      <description>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 &amp;lt; /foo.txt" part!&lt;BR /&gt;&lt;BR /&gt;Thanks so much.&lt;BR /&gt;&lt;BR /&gt;Pat&lt;BR /&gt;</description>
      <pubDate>Wed, 14 Apr 2010 17:55:02 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235045#M675695</guid>
      <dc:creator>RoyP</dc:creator>
      <dc:date>2010-04-14T17:55:02Z</dc:date>
    </item>
    <item>
      <title>Re: while read line iteration cause variable value reset?</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235046#M675696</link>
      <description>Closed as per James's solution.</description>
      <pubDate>Wed, 14 Apr 2010 17:55:54 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-read-line-iteration-cause-variable-value-reset/m-p/5235046#M675696</guid>
      <dc:creator>RoyP</dc:creator>
      <dc:date>2010-04-14T17:55:54Z</dc:date>
    </item>
  </channel>
</rss>

