title: shell script to ssh remote machines and print the output of the top command
body: <p>I want to write a shell script to do the following four things: </p>
  
  <ol>
  <li>ssh a remote machine (say hosti)  </li>
  <li>print the machine name to a file (top_out)  </li>
  <li>print the first few lines of the output of the 'top' command to the same file as in step2  </li>
  <li>repeat 1-3 for an other machine</li>
  </ol>
  
  <p>I tried this:</p>
  
  <pre><code>#! /bin/bash
  
  for i in 1 2 3 4 5 6 7 8
  
  do
      echo "host$i" &gt;&gt; ~/mysh/top_out
      ssh host$i "top -n1 -b | head -n 15&gt;&gt; ~/mysh/top_out"
      echo "done"
  done
  </code></pre>
  
  <p>The output file that I got had saved the top output for some machines (say like host5-8), but it was blank for the early machinessay like host1-4. If I tried without the line "echo "host$i" >> ~/mysh/top_out", I can get the top output for all the host1-8. </p>