Now imagine a GPRS link running at 9.600 bit/s and a 5 GByte file. The first line will run for days, fill your local memory and your swap, just to give you the information that the logfile existed when you started the cat command and had at least two lines.

To fix the two-lines problem, first compare with 0, not 1. This will give you a true value if the file exists and is not empty.

To really fix the problem, don't transfer the file at all. Use the remote shell:

my $flag=`ssh REMOTE_SERVER [ -f /tmp/somefile.log ] && echo 1`; chomp $flag; # not really needed

This will download at most two bytes, independant from the file size.

What happens here?

  1. ssh opens a shell to REMOTE_SERVER, then passes all remaining arguments to the shell.
  2. [ ] is a shortcut for the test command, the -f argument tells test to check if the next argument is the name of an existing file, and to return either success or failure. test is completely silent, it does not generate any output.
  3. && tells the shell to execute the right-hand-side command (echo) only if the left-hand-side command (test) returned success.
  4. echo 1 writes the digit 1 and a newline character to STDOUT.
  5. The backticks (``) collect what is written to STDOUT, dropping what is written to STDERR.
  6. Depending on the existence of a file named /tmp/somefile.log, $flag contains either "" (false) or "1\n" (true).
  7. chomp strips that final "\n".

Some notes:

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

In reply to Re: check for a remote file by afoken
in thread check for a remote file by Furple

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.