<?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: Script for loops, with multiple files in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183290#M163301</link>
    <description>Hi,&lt;BR /&gt;&lt;BR /&gt;for one, I would assign different loop variables for nested loops to avoid confusion. Then the return code of grep is 1 when no match was there. You have to compare to 0. Joint the two grep like grep -q "${HOST}.*changed passwd" ${i}&lt;BR /&gt;&lt;BR /&gt;Michael&lt;BR /&gt;</description>
    <pubDate>Wed, 04 Feb 2004 17:07:21 GMT</pubDate>
    <dc:creator>Michael Schulte zur Sur</dc:creator>
    <dc:date>2004-02-04T17:07:21Z</dc:date>
    <item>
      <title>Script for loops, with multiple files</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183288#M163299</link>
      <description>I know I need a for loop but...&lt;BR /&gt;This is what I have...&lt;BR /&gt;&lt;BR /&gt;5 scripts, same name except it is script_1, script_2 etc.&lt;BR /&gt;&lt;BR /&gt;Each of these scripts gives an output to a file... output1, output2, etc.&lt;BR /&gt;&lt;BR /&gt;Instead of making variables for each file, I thought I can do a for statement...&lt;BR /&gt;&lt;BR /&gt;I want to run each script, then check the output of each output and if it contains anything mail me.&lt;BR /&gt;I got the check for output part, thanks to S. Bhaskarla&lt;BR /&gt;&lt;BR /&gt;But when I run what I have I get multiple entries and multiple emails!&lt;BR /&gt;&lt;BR /&gt;I only want the 5 emails if there is data...&lt;BR /&gt;  &lt;BR /&gt;  &lt;BR /&gt;find $SCRIPTS_DIR/reset_pass* -print &amp;gt; /tmp/reset_pass.input&lt;BR /&gt;find $LOGS_DIR/login_entry_* -print &amp;gt; /tmp/login_entry.input&lt;BR /&gt;  &lt;BR /&gt;for i in `cat /tmp/reset_pass.input`&lt;BR /&gt;do&lt;BR /&gt;        ${i} #run the script&lt;BR /&gt;        rm -r /tmp/host.not&lt;BR /&gt;        for i in `cat /tmp/login_entry.input`&lt;BR /&gt;        do&lt;BR /&gt;        for HOST in $(awk '{print $1}' $SCRIPTS_DIR/sw_ip.txt |sort|uniq)&lt;BR /&gt;        do&lt;BR /&gt;                echo ${i}&lt;BR /&gt;                grep ${HOST} ${i} |grep -q "changed passwd"&lt;BR /&gt;                if [ ${?} = 1 ]&lt;BR /&gt;                then&lt;BR /&gt;                   echo $HOST &amp;gt;&amp;gt;/tmp/host.not&lt;BR /&gt;                   mail -s "${i}, errors" $MAILTO &amp;lt; /tmp/host.not&lt;BR /&gt;                fi&lt;BR /&gt;                done&lt;BR /&gt;        done&lt;BR /&gt;done&lt;BR /&gt;</description>
      <pubDate>Wed, 04 Feb 2004 16:57:40 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183288#M163299</guid>
      <dc:creator>Ratzie</dc:creator>
      <dc:date>2004-02-04T16:57:40Z</dc:date>
    </item>
    <item>
      <title>Re: Script for loops, with multiple files</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183289#M163300</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;If you looked at my first response in the other post, I am checking for a variable called "STAMP" to see if it is set to 1. If it sets, then I will send the mail with the contents of the temporary file. I set STAMP to 1 when I find a case. This is your modified script to add that functionality.&lt;BR /&gt;&lt;BR /&gt;find $SCRIPTS_DIR/reset_pass* -print &amp;gt; /tmp/reset_pass.input&lt;BR /&gt;find $LOGS_DIR/login_entry_* -print &amp;gt; /tmp/login_entry.input&lt;BR /&gt;&lt;BR /&gt;for i in `cat /tmp/reset_pass.input`&lt;BR /&gt;do&lt;BR /&gt;${i} #run the script&lt;BR /&gt;rm -r /tmp/host.not&lt;BR /&gt;STAMP=0&lt;BR /&gt;for i in `cat /tmp/login_entry.input`&lt;BR /&gt;do&lt;BR /&gt;for HOST in $(awk '{print $1}' $SCRIPTS_DIR/sw_ip.txt |sort|uniq)&lt;BR /&gt;do&lt;BR /&gt;echo ${i}&lt;BR /&gt;grep ${HOST} ${i} |grep -q "changed passwd"&lt;BR /&gt;if [ ${?} = 1 ]&lt;BR /&gt;then&lt;BR /&gt;echo $HOST &amp;gt;&amp;gt;/tmp/host.not&lt;BR /&gt;STAMP=1&lt;BR /&gt;fi&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;if [ $STAMP = 1 ]&lt;BR /&gt;then&lt;BR /&gt;mail -s "${i}, errors" $MAILTO &amp;lt; /tmp/host.not&lt;BR /&gt;fi&lt;BR /&gt;&lt;BR /&gt;done&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;-Sri&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 04 Feb 2004 17:05:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183289#M163300</guid>
      <dc:creator>Sridhar Bhaskarla</dc:creator>
      <dc:date>2004-02-04T17:05:04Z</dc:date>
    </item>
    <item>
      <title>Re: Script for loops, with multiple files</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183290#M163301</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;for one, I would assign different loop variables for nested loops to avoid confusion. Then the return code of grep is 1 when no match was there. You have to compare to 0. Joint the two grep like grep -q "${HOST}.*changed passwd" ${i}&lt;BR /&gt;&lt;BR /&gt;Michael&lt;BR /&gt;</description>
      <pubDate>Wed, 04 Feb 2004 17:07:21 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183290#M163301</guid>
      <dc:creator>Michael Schulte zur Sur</dc:creator>
      <dc:date>2004-02-04T17:07:21Z</dc:date>
    </item>
    <item>
      <title>Re: Script for loops, with multiple files</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183291#M163302</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;Michael's point is to be noted. 'i' is defined in first and second for loops. While it may work in this particular case, it is not a very good idea in general. It's better to use some meaningful names like 'for SCRIPT in `cat /tmp/reset_pass.input`, 'for ENTRY in `cat /tmp/login_entry.input` etc., Also, Posix and shell allow $() for command substitution so you don't have to worry about the ticks that get you confused sometimes with other quotes.&lt;BR /&gt;&lt;BR /&gt;There is a small correction in my previous post. Put "STAMP=0" just above 'for HOST' loop.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;for i in `cat /tmp/reset_pass.input`&lt;BR /&gt;do&lt;BR /&gt;${i} #run the script&lt;BR /&gt;rm -r /tmp/host.not&lt;BR /&gt;for i in `cat /tmp/login_entry.input`&lt;BR /&gt;do&lt;BR /&gt;STAMP=0&lt;BR /&gt;for HOST in $(awk '{print $1}' $SCRIPTS_DIR/sw_ip.txt |sort|uniq)&lt;BR /&gt;do&lt;BR /&gt;&lt;BR /&gt;Rest remains the same.&lt;BR /&gt;&lt;BR /&gt;It is a good idea to use {} while working with variables. But my personal preference is to use them only when needed. For ex., if I have to define a file name outputfile.${SCRIPT}.${DATE} to cause less confusion. However, I don't use {} convention necessarily for simple checks (if [ $? = 0 ])as it may be more confusing there.&lt;BR /&gt;&lt;BR /&gt;Multiple greps can be combined into one. &lt;BR /&gt;&lt;BR /&gt;-Sri&lt;BR /&gt;</description>
      <pubDate>Wed, 04 Feb 2004 17:22:52 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183291#M163302</guid>
      <dc:creator>Sridhar Bhaskarla</dc:creator>
      <dc:date>2004-02-04T17:22:52Z</dc:date>
    </item>
    <item>
      <title>Re: Script for loops, with multiple files</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183292#M163303</link>
      <description>I have modified my script and now I am in an endless loop, and I recieve endless emails...&lt;BR /&gt;&lt;BR /&gt;All I should be getting is 5 emails, and only showing the IPs that do not have "change passwd"&lt;BR /&gt;&lt;BR /&gt;Really appreciate another pair of eyes.</description>
      <pubDate>Wed, 04 Feb 2004 18:57:02 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183292#M163303</guid>
      <dc:creator>Ratzie</dc:creator>
      <dc:date>2004-02-04T18:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: Script for loops, with multiple files</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183293#M163304</link>
      <description>Hi,&lt;BR /&gt;&lt;BR /&gt;rm -r /tmp/host.not # delete the -r&lt;BR /&gt;No need to delete a single file recursively&lt;BR /&gt;&lt;BR /&gt;sort|uniq can be shortened to sort -u&lt;BR /&gt;&lt;BR /&gt;$? = 1 should be $? = 0 # see man grep for return codes&lt;BR /&gt;&lt;BR /&gt;I really can't see, why you should get an endless loop. May be a lot of loops, but the script should terminate.&lt;BR /&gt;&lt;BR /&gt;Michael&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Thu, 05 Feb 2004 04:07:53 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183293#M163304</guid>
      <dc:creator>Michael Schulte zur Sur</dc:creator>
      <dc:date>2004-02-05T04:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: Script for loops, with multiple files</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183294#M163305</link>
      <description>The maximum number of mails you get is the number of entries in /tmp/reset_pass.input times the number of entries in /tmp/login_entry.input.&lt;BR /&gt;&lt;BR /&gt;If that's not what you want, you need to check the creation of those files.&lt;BR /&gt;&lt;BR /&gt;What you could do is add an echo to the mail line and then redirect the output of the script to a file and look at the size of the resulting files.&lt;BR /&gt;&lt;BR /&gt;You could also skip all real actions and just print your loop variables to see if those are flowing correctly.&lt;BR /&gt;&lt;BR /&gt;I see no usage of the outer loop variable in the inner loop variables, so the internal loop (with LOGIN_ENTRY) will result in the same output for all occurrences of the outer loop (with SCRIPT_EXP). Perhaps that's causing your problem?&lt;BR /&gt;&lt;BR /&gt;And one more thing: you can send a mail for every LOGIN_ENTRY occurrence for every SCRIPT_EXP... Perhaps you should put that part after the 'done' line for the LOGIN_ENTRY loop, instead of before?&lt;BR /&gt;Because the removal of the host.not file is done before the 'for LOGIN_ENTRY' line, that seems what you want... Or you should put the removal of host.not after the 'for LOGIN_ENTRY' line.</description>
      <pubDate>Thu, 05 Feb 2004 05:41:13 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/script-for-loops-with-multiple-files/m-p/3183294#M163305</guid>
      <dc:creator>Elmar P. Kolkman</dc:creator>
      <dc:date>2004-02-05T05:41:13Z</dc:date>
    </item>
  </channel>
</rss>

