in reply to Re: Get remote file TimeStamp
in thread Get remote file TimeStamp

This was fine on Linux/bash , but not HP-UX/ksh. I then got this working at the command line

ssh -q aimuat perl -e \'\$MTIME = \( stat \( \"Envs_aimuat.xml\" \)\)\[9\]\;print \$MTIME\;\'

But when I put in into a perl script it fails

$TStamp=`ssh -v aimuat perl -e '\$MTIME = \( stat \( \"Envs_aimuat.xml +\" \)\)\[9\]\;print \$MTIME\;'`; print "$TStamp";
Any Ideas? Thanx

Replies are listed 'Best First'.
Re^3: Get remote file TimeStamp
by Eliya (Vicar) on Feb 29, 2012 at 18:10 UTC

    The backticks "eat" one level of escapes, when they occur literally within the backticks.  So, you either need to double escape them, or maybe better (to work around the ugliness), put the command in a variable, which you then interpolate into the backticks:

    my $cmd = q|ssh -q aimuat perl -e \'\$MTIME = \( stat \( \"Envs_aimuat +.xml\" \)\)\[9\]\;print \$MTIME\;\'|; $TStamp = `$cmd`;

    P.S., you can do away with the $MTIME variable if you use a "+" to syntactically disambiguate the argument to the print function:

    my $cmd = q|ssh -q aimuat perl -e \'print +\( stat \( \"Envs_aimuat.xm +l\" \)\)\[9\]\;\'|; $TStamp = `$cmd`;

      This works well, and since I cannot add modules, it is preferred. My last problem which I am still working on is that the "aimuat" in the name argument of the ssh, and farther on in the file name needs to be a variable.

      foreach $SYSTEM (ifeuu1, ifcau1, ifcaa1, mifuat, aimuat, timeuat) { my $cmd = q|ssh -q $SYSTEM perl -e \'print +\( stat \( \"Envs_$SYSTEM. +xml\" \)\)\[9\]\;\'|; print `$cmd`; }

      Thanx for all your help, it is much appreciated

        There are several ways to solve this.  For one, you could use the interpolating qq operator (acts like double quotes), but then you'd have to double escape things...  Personally, I would rather use concatenation, which isn't pretty either, but somewhat easier to read (IMHO):

        foreach my $SYSTEM (qw(ifeuu1 ifcau1 ifcaa1 mifuat aimuat timeuat)) { my $cmd = 'ssh -q '.$SYSTEM.q| perl -e \'print +\( stat \( \"Envs_ +|.$SYSTEM.q|.xml\" \)\)\[9\]\;\'|; print `$cmd`; }

        Also note that I used qw() for the hostname list, because what you had were barewords, usage of which is generally discouraged.  qw(foo bar baz) is just a short form of saying 'foo', 'bar', 'baz'.