in reply to Comparing localtime to a remote file's modification time.

$FileTime = `ssh $ServerName 'perl -e '(stat($FP_RemoteFile))[9];''`; ^ ^ | | this is quote closed here

You need to escape the 2nd and 3rd single quote. I don't know how to do that in your shell. Perhaps you need to preceed them by \\ (one slash for the shell, and a second one to escape the shell's slash for Perl).

Replies are listed 'Best First'.
Re^2: Comparing localtime to a remote file's modification time.
by baerlun (Initiate) on Oct 23, 2006 at 20:41 UTC
    I'm using bourne for this... but escaping the single quotes like this: $FileTime = `ssh $ServerName \'perl -e \'(stat($FP_RemoteFile))[9];''`; or $FileTime = `ssh $ServerName \'perl -e \'(stat($FP_RemoteFile))[9];\'\'`; gives me the same syntax errors. (sh: syntax error at line 1: `(' unexpected) It looks as though the shell is actually complaining and, therefore, I guess I should assume that the perl one-liner format (quoting) is incorrect? In this, Ikegami, you may be on the right track. I'd love to put this in a script on the remote machine and run it from our watch server but then I'd need tiny scripts on every server that needs to be watched. This would make things a bit too complex to maintain. Is there no way to stat a remote file using ssh from within a perl script and capture the output?

      When Perl sees \' in a string literal, it places just ' in the string, so you haven't changed anything.

      You seem to have missed half of my comment.
      I didn't suggest that you escape the 1st and 2nd ' with \.
      I didn't suggest that you escape all the ' with \.
      I suggested that you escape the 2nd and 3rd ' with \\.

      $FileTime = `ssh $ServerName 'perl -e \\'(stat($FP_RemoteFile))[9];\\''`;
      executes
      ssh $ServerName 'perl -e \'(stat(...))[9];\''
      which executes
      perl -e '(stat(...))[9];'
      on the remote machine if sh behaves as I think it does.

      Update: stat($FP_RemoteFile) is also wrong. You need to convert the contents of $FP_RemoteFile into a Perl literal, then you must escape it for sh.

        You're right, of course (about both: my lack of reading comprehension and the explanation). I guess thinking it through should've worked for me. Thanks again.