Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am trying to run while loop with read through ssh:

#!/bin/bash

ssh root@10.10.10.10 "cat /var/log/syncer/rm_filesystem.log | while read path; do stat -c \"%Y %n\" "$path"  >> /tmp/fs_10.10.10.10.log done"

But the issue is my variable $path is resolving on my localhost where as I want to resolve it on remote host so that it can read file on remote host and take stat of all folder/files listed in "rm_filesystem.log"

share|improve this question
add comment

3 Answers

or put the ssh command line into single quotes rather than double. then the command will be passed as is without any substitution at the local end. i.e.

ssh root@10.10.10.10 'cat /var/log/syncer/rm_filesystem.log | while read path; do stat -c "%Y %n" "$path"  >> /tmp/fs_10.10.10.10.log done'
share|improve this answer
add comment

ssh user@host "bash -s" < yourscript.sh

What is currently happening is that the script is being parsed on your local machine and then sent in its entirety to the remote machine. So by the time it gets there, the $PATH variable will have already been replaced by your local $PATH value.

What the example above does is open a bash session on your remote machine and set bash to read from stdin (see http://www.gnu.org/software/bash/manual/bashref.html#Invoking-Bash)

You then pass in the script (which you have typed into an external file) so that it executes directly on the remote machine.

This way, there is no local parsing of the script, and thus no replacement of the environment variable.

share|improve this answer
add comment
up vote 0 down vote accepted

I made it working in a good way as it should be:

!/bin/bash

ssh root@10.10.10.10 "cat /var/log/syncer/rm_filesystem.log | while read path; do stat -c '%Y "%n"' "\"'$path'\"" >> /tmp/fs_10.10.10.10.log done"

After reading bash man page I got this trick ..

Thanks everyone for help

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.