<?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 loop ends after SSH command in Operating System - HP-UX</title>
    <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304387#M881276</link>
    <description>I had the same problem a while ago and the `for i" worked for me instead of the while do loop.&lt;BR /&gt;&lt;BR /&gt;So the above solution should work.&lt;BR /&gt;for line in `cat text_file.txt`&lt;BR /&gt;do&lt;BR /&gt;srvr=`echo $line | awk '{print $1}'`&lt;BR /&gt;ssh $line "echo test"&lt;BR /&gt;done&lt;BR /&gt;</description>
    <pubDate>Mon, 14 Jun 2004 11:13:28 GMT</pubDate>
    <dc:creator>Abdul Rahiman</dc:creator>
    <dc:date>2004-06-14T11:13:28Z</dc:date>
    <item>
      <title>While loop ends after SSH command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304385#M881274</link>
      <description>Hello o wizards &amp;amp; pharaohs,&lt;BR /&gt;&lt;BR /&gt;You gracious prior help has brought me back again asking for divine guidance.  &lt;BR /&gt;&lt;BR /&gt;I have a â  while loopâ   from reading a simple text file which I use to send commands to remote servers via ssh.  &lt;BR /&gt;&lt;BR /&gt;The problem is that after the first iteration the loop ends.  It won't end if I don't do an ssh call.  Iâ  ve tried this with simple echo commands and that works fine, goes through all lines in the text file.&lt;BR /&gt;&lt;BR /&gt;# --------------------------------------------&lt;BR /&gt;Here is the text file:&lt;BR /&gt;# Simple Text File&lt;BR /&gt;appserver1 domain1&lt;BR /&gt;appserver1 domain2&lt;BR /&gt;appserver2 domain1&lt;BR /&gt;appserver2 domain2&lt;BR /&gt;â ¦&lt;BR /&gt;# -------------------------------------------&lt;BR /&gt;Script:&lt;BR /&gt;# Simple while loop on text file contents&lt;BR /&gt;&lt;BR /&gt;cat text_file.txt | while read LINE&lt;BR /&gt;do&lt;BR /&gt;&lt;BR /&gt;  export srvr=$(cat $LINE|awk -F '{print $1}')&lt;BR /&gt;  export domn=$(cat $LINE|awk -F '{print $2}')&lt;BR /&gt;&lt;BR /&gt;  ssh $srvr ps -ef|grep $domn&lt;BR /&gt;&lt;BR /&gt;done&lt;BR /&gt;# -------------------------------------------&lt;BR /&gt;&lt;BR /&gt;The first iteration always works.  With the ssh command the loop simply ends and won't go to the next value in the while loop - no errors.&lt;BR /&gt;&lt;BR /&gt;What the heck am I missing?&lt;BR /&gt;&lt;BR /&gt;Thanks in advance,&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 14 Jun 2004 10:46:49 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304385#M881274</guid>
      <dc:creator>Marly Thomas</dc:creator>
      <dc:date>2004-06-14T10:46:49Z</dc:date>
    </item>
    <item>
      <title>Re: While loop ends after SSH command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304386#M881275</link>
      <description>Script:&lt;BR /&gt;# Simple while loop on text file contents&lt;BR /&gt;&lt;BR /&gt;cat text_file.txt | while read LINE&lt;BR /&gt;do&lt;BR /&gt;&lt;BR /&gt;export srvr=$(cat $LINE|awk -F '{print $1}')&lt;BR /&gt;export domn=$(cat $LINE|awk -F '{print $2}')&lt;BR /&gt;&lt;BR /&gt;ssh $srvr "ps -ef|grep $domn"&lt;BR /&gt;&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;OR&lt;BR /&gt;&lt;BR /&gt;for i in `cat file`&lt;BR /&gt;do&lt;BR /&gt;ssh `export srvr=$(cat $LINE|awk -F '{print $1}')` "`export domn=$(cat $LINE|awk -F '{print $2}')`"&lt;BR /&gt;done&lt;BR /&gt;&lt;BR /&gt;Anil</description>
      <pubDate>Mon, 14 Jun 2004 11:04:56 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304386#M881275</guid>
      <dc:creator>RAC_1</dc:creator>
      <dc:date>2004-06-14T11:04:56Z</dc:date>
    </item>
    <item>
      <title>Re: While loop ends after SSH command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304387#M881276</link>
      <description>I had the same problem a while ago and the `for i" worked for me instead of the while do loop.&lt;BR /&gt;&lt;BR /&gt;So the above solution should work.&lt;BR /&gt;for line in `cat text_file.txt`&lt;BR /&gt;do&lt;BR /&gt;srvr=`echo $line | awk '{print $1}'`&lt;BR /&gt;ssh $line "echo test"&lt;BR /&gt;done&lt;BR /&gt;</description>
      <pubDate>Mon, 14 Jun 2004 11:13:28 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304387#M881276</guid>
      <dc:creator>Abdul Rahiman</dc:creator>
      <dc:date>2004-06-14T11:13:28Z</dc:date>
    </item>
    <item>
      <title>Re: While loop ends after SSH command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304388#M881277</link>
      <description>There definately used to be a bug in the posix shell that made while loops do this kind of thing but I thought it was sorted a long time ago.&lt;BR /&gt; &lt;BR /&gt;However, you might try quoting your ssh command.  I can't see how that pipe might break something but it might.  Perhaps try &lt;BR /&gt; &lt;BR /&gt; ssh $srvr "ps -ef|grep $domn".  Obviously the grep will now be done on the remote site but I guess that doesn't matter too much.</description>
      <pubDate>Mon, 14 Jun 2004 11:35:48 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304388#M881277</guid>
      <dc:creator>Mark Grant</dc:creator>
      <dc:date>2004-06-14T11:35:48Z</dc:date>
    </item>
    <item>
      <title>Re: While loop ends after SSH command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304389#M881278</link>
      <description>Thanks for the reply guys!&lt;BR /&gt;&lt;BR /&gt;I solved this a bit differently, however.  I couldn't use the for loop because it gave me the items in the text file either as a single string seperated by a space or the whole file in one line (using quotes).&lt;BR /&gt;&lt;BR /&gt;So what I did was to submit the ssh command into the background - the output goes into a temp file anyway.  So here is the result:&lt;BR /&gt;&lt;BR /&gt;Here is the text file:&lt;BR /&gt;# Simple Text File&lt;BR /&gt;appserver1 domain1&lt;BR /&gt;appserver1 domain2&lt;BR /&gt;appserver2 domain1&lt;BR /&gt;appserver2 domain2&lt;BR /&gt;â ¦&lt;BR /&gt;-------------------------------------------&lt;BR /&gt;Script:&lt;BR /&gt;&lt;BR /&gt;# Simple while loop on text file contents&lt;BR /&gt;&lt;BR /&gt;cat text_file.txt | while read LINE&lt;BR /&gt;do&lt;BR /&gt;&lt;BR /&gt;  export srvr=$(cat $LINE|awk -F '{print $1}')&lt;BR /&gt;  export domn=$(cat $LINE|awk -F '{print $2}')&lt;BR /&gt;&lt;BR /&gt;  ssh $srvr ps -ef|grep $dom &amp;gt;&amp;gt; /tmp/results.txt &amp;amp;&lt;BR /&gt;&lt;BR /&gt;done&lt;BR /&gt;-----------------------------------------</description>
      <pubDate>Mon, 14 Jun 2004 12:07:09 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304389#M881278</guid>
      <dc:creator>Marly Thomas</dc:creator>
      <dc:date>2004-06-14T12:07:09Z</dc:date>
    </item>
    <item>
      <title>Re: While loop ends after SSH command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304390#M881279</link>
      <description>What was occuring is that the STDIN was getting spat anywhere into the subshell created by the pipe.&lt;BR /&gt;&lt;BR /&gt;So when you SSH'd in, the STDIN came open again, and sucked the rest of the contents of the 'cat'.&lt;BR /&gt;&lt;BR /&gt;You can mess around with file-descriptors in order to get it to work properly, but backgrounding seems just as good a solution ;)</description>
      <pubDate>Mon, 14 Jun 2004 18:44:04 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304390#M881279</guid>
      <dc:creator>Stuart Browne</dc:creator>
      <dc:date>2004-06-14T18:44:04Z</dc:date>
    </item>
    <item>
      <title>Re: While loop ends after SSH command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304391#M881280</link>
      <description>Try running ssh with the -n switch.&lt;BR /&gt;This solves the problem with stdin.&lt;BR /&gt;&lt;BR /&gt;Hope this helps,&lt;BR /&gt;Armin&lt;BR /&gt;</description>
      <pubDate>Tue, 15 Jun 2004 03:48:38 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304391#M881280</guid>
      <dc:creator>Armin Kunaschik</dc:creator>
      <dc:date>2004-06-15T03:48:38Z</dc:date>
    </item>
    <item>
      <title>Re: While loop ends after SSH command</title>
      <link>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304392#M881281</link>
      <description>Hello - here is a followup.&lt;BR /&gt;&lt;BR /&gt;The backgrounding had some other side effects - I could only run so many at a time and many didn't return (I have 30 to run in total).  So I was excited when I got in this morning to see some more responses to my problem.&lt;BR /&gt;&lt;BR /&gt;The stdin explanation and the -n option is the coup de grace to the problem.  I removed the backgrounding and put in the -n option and it worked great.  Thanks!&lt;BR /&gt;&lt;BR /&gt;Marly Thomas</description>
      <pubDate>Tue, 15 Jun 2004 09:48:27 GMT</pubDate>
      <guid>https://community.hpe.com/t5/operating-system-hp-ux/while-loop-ends-after-ssh-command/m-p/3304392#M881281</guid>
      <dc:creator>Marly Thomas</dc:creator>
      <dc:date>2004-06-15T09:48:27Z</dc:date>
    </item>
  </channel>
</rss>

