Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i wish to check if a logfile on the server is non-zero. this file is originally created with information such as product name etc. when the script is run for the first time product name etc gets added. subsequently when i run the script i want to check whether this file is non-zero.. this is to avoid the redundant product name, etc appended redundantly. i am using ssh auto authentication for access to files. here a piece of code i am stuck at:

... if (-s "$ipaddress:/$dirlocation/$logfile") {
print "File exists..\n";
} else {
system ("ssh $ipaddress touch /$dirlocation/$logfile");
system ("ssh $ipaddress print $productname '>>' /$dirlocation/$logfile");
}

i understand the conditions in my if statement is wrong. hope someone can help. thanks!

Replies are listed 'Best First'.
Re: check if file on network is non-zero
by iburrell (Chaplain) on Nov 06, 2002 at 20:46 UTC
    You can run the bash shell test program on the remote machine. It works just like the Perl test operators and ssh will return its return status.
    unless (system('ssh', $host, 'test -s $file') == 0) {

    You will need to be careful checking the return status of system because 0 is true and anything else is false. And -s checks if the file exists and has non-zero size.

    Also, there is no print command; the shell equivalent is echo. You don't need to touch the file first; changing it will update the modtime. Do you really want to append to the file? It either doesn't exist or is zero size so you can use '>' to overwrite the file. I find that especially with ssh, it is better to use the array form of ssh because it makes explicit what is the remote command and you don't have to worry about quoting.

    system('ssh', $host, "echo $product > $file");

    Even better would be to use the power of the shell and dothe entire operation on the remote machine with a single ssh call.

    system('ssh', $host, "if [ ! -s $file ]; then echo $product > $file; f +i");