I want to write a shell script to do the following four things:
- ssh a remote machine (say hosti)
- print the machine name to a file (top_out)
- print the first few lines of the output of the 'top' command to the same file as in step2
- repeat 1-3 for an other machine
I tried this:
#! /bin/bash
for i in 1 2 3 4 5 6 7 8
do
echo "host$i" >> ~/mysh/top_out
ssh host$i "top -n1 -b | head -n 15>> ~/mysh/top_out"
echo "done"
done
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.
echo > ~/mysh/top_out. Good luck.ssh host$i "top -n1 -b | head -n 15>> ~/mysh/top_out"to this:ssh host$i "top -n1 -b | head -n 15 " >> ~/mysh/top_out, ie, the>> ~/mysh/top_outneeds to be outside the quotes.