Need help on for loop inside ssh

Hi,

I am having a file like,

#cat file
Jun 19 13:08
Jun 19 13:08
Jun 19 13:08
Jun 19 13:14

when I run the below comamnd locally it will work fine,

IFS=$'\n'; for i in $(cat file) ;do echo "HI $i" ; done

And the output is,

HI Jun 19 13:08
HI Jun 19 13:08
HI Jun 19 13:08
HI Jun 19 13:14

But when i run the same command inside ssh,

ssh root@IPaddress "IFS=$'\n'; for i in $(cat file) ;do echo "HI $i" ; done"

I am getting an error saying,

sh: syntax error at line 2: `Jun' unexpected

TIA

Think it over: when you do a "ssh machine command", then "command" will be executed on the remote host, not your local one. If "command" is something like "for x in $(cat file) ...", then you are referring to a remote file, not a local one!

if you want to process a file locally and start ssh-sessions remotely using the content of the file you have to do the loop locally. The following sketch shows how:

while read LINE ; do
     ssh user@host "do_something_with $LINE"
done < /path/to/some/file

A second problem is your use of double quotes. You see, a shell maintains a simple switch "inside/outside a double quote" and once it encounters such a character this switch is flipped. Therefore double quotes cannot be nested. You will have to escape the inner pair to make your command work:

ssh root@IPaddress "IFS=$'\n'; for i in $(cat file) ;do echo \"HI $i\" ; done"

(which will - as explained above - still not work, but for a different reason)

I hope this helps.

bakunin