LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Perl: system() mail losing a variable value (https://www.linuxquestions.org/questions/programming-9/perl-system-mail-losing-a-variable-value-90367/)

m0rl0ck 09-07-2003 11:08 PM

Perl: system() mail losing a variable value
 
Im using the backtick operator to get the size in megabytes of a dir like so:

$exit_value = qx/du -sh \/mnt\/cdrom|cut -f1|tr A-Z " "/;


and in this function:


sub FileAge{

system("echo \"TOTALSIZE IS $exit_value\"|mail -s test m0rl0ck");

print "TOTALSIZE IS $exit_value";

@atime = stat($_[0]);

if (time() - $atime[9] > 1800){
die "$errormsg file " . $_[0] . " more than 30 minutes old";
}
}


The system line emails me no value for $exit_value, the mail I get contains:

TOTALSIZE IS

and no number value, the print statement works fine. What is system() or mail doing to $exit_value? Anyone know how to preserve the value of $exit_value so I can email it to myself using the system functon?

david_ross 09-08-2003 12:40 PM

Well in the example you gave there are no backtics whihc seems odd. If you are looking for the actual exit value then it will be in "$?". If you are looking for the return value then it should be in the variable. If the value is being returned to stderr you may need to redirect stderr to stdout "2>&1"

m0rl0ck 09-08-2003 04:47 PM

Quote:

Well in the example you gave there are no backtics whihc seems odd.
Backticks are customary but not mandatory. From man perlop:
Customary Generic Meaning Interpolates
'' q{} Literal no
"" qq{} Literal yes
`` qx{} Command yes*

And you can pick just about anything for a delimiter you want so qx/ / should be getting the value of exit value and it does work in the print statement.
Quote:

If you are looking for the actual exit value then it will be in "$?".
Im not. it isnt returning an error, its value just isnt surviving the system(mail) function.

Quote:

If you are looking for the return value then it should be in the variable.
Yes, but it isnt, puzzleing isnt it?
Quote:

If the value is being returned to stderr you may need to redirect stderr to stdout "2>&1"
Im willing to give it a shot :) How would I do that in this case?

david_ross 09-09-2003 12:27 PM

Well I've just run a test and from what you have it seems to work for me. What version of perl are you using? Can you post the whole script.

m0rl0ck 09-09-2003 01:26 PM

Thanks for your intrest, im puzzled by this. I actually gave up and started using (stat()[7]/1024)/1024 to get the size of individual files and added the sizes to get a total, which works. Still Id like to know whats happening here.
Heres the test script: perl 5.8.0




&FileAge("/mnt/cdrom/etc-bunny.tar.gz");

@sarry=stat("/mnt/cdrom/etc-bunny.tar.gz");

$sm=(@sarry[7]/1024)/1024;

print $sm;


$errormsg="BACKUP FAILED";





$exit_value = qx/du -sh \/mnt\/cdrom|cut -f1|tr A-Z " ">ftmp/;




$exit_value=~s/\n//g;

# print "TOTALSIZE IS$exit_value" . "blarg";





sub FileAge{



system("echo \"TOTALSIZE IS $exit_value\"|mail -s \"test $?\" m0rl0ck");

print "TOTALSIZE IS $exit_value";
@atime = stat($_[0]);
if (time() - $atime[9] > 16000){
die "$errormsg file " . $_[0] . " more than 10 minutes old";
}
}



The print line works. The mail line baive a message with a body of:

TOTALSIZE IS

No value for $exit_value :)

EDIT: forgot to add, cdrom is mounted for the above.

david_ross 09-09-2003 02:03 PM

Exactly which print statement works? There is no line calling the subroutine after $exit_value is created.

m0rl0ck 09-09-2003 02:22 PM

Quote:

Exactly which print statement works?


The print statement in the sub works:

print "TOTALSIZE IS $exit_value";

david_ross 09-09-2003 02:47 PM

I can't get even that to work wit hthe example you have given since the subroutine is nver called after the variable is generated.
Try this:
#!/usr/bin/perl

$exit_value = qx/du -sh \/mnt\/cdrom|cut -f1|tr A-Z " "/;
print "TOTALSIZE IS $exit_value";
system("echo \"TOTALSIZE IS $exit_value\"|mail -s \"test\" m0rl0ck");

exit;

m0rl0ck 09-09-2003 06:40 PM

Its working as you suggested with what is posted here. Im having trouble reconstructing the original problem because I changed the original script so much. I can see why $exit_value would have to be defined before the sub was called and I think normally that would have occurred to me (duh!) but I swear to you I actually had a script working (sort of) where the print statement $exit_value worked and the system(mail) $exit_value wouldnt.
I may try to put it back together and will post it if I can.

What I decided to use instead, which I like better is:

# test each of the above files

foreach $cdfile(@filetestlist){


######## remove \n s

if ($cdfile=~/\n/){

$cdfile=~s/\n//g
}



@totalsize=stat($cdfile);

$totalsizeread=@totalsize[7]+$totalsizeread;







&FileTest($cdfile);
}


$totalsizeread=($totalsizeread/1024)/1024;
$today=localtime();


system("echo \"Files @filetestlist written to cd. \n Total size is $totalsizeread megabytes.\"|mail -s \"Backups for $today Successful\" m0rl0ck");


and replaced FileAge with:
sub FileTest{


############ time testing -- If file is more than 12 hours old
# send an error message


@atime = stat($_[0]);

if (time() - $atime[9] > 43200){



system("echo \"file $_[0] more than 12 hours old\"|mail -s \"BACKUPS for $today FAILED\" m0rl0ck");
die "BACKUPS FAILED file " . $_[0] . " more than 12 hours old";





}

############# If its an iso also test it for size ###
#

if ($_[0]=~/iso/){




$isosize=qx/du -sh $_[0]|cut -f1|tr A-Z " "/;

print "ISOSIZE is $isosize";


if ($isosize>690){
system("echo \"file $_[0] TOO LARGE TO WRITE TO CD\"|mail -s \"BACKUPS FAILED\" m0rl0ck");
die "BACKUPS FAILED file " . $_[0] . " TOO LARGE TO WRITE TO CD";

}

}


} #end FileTest







All times are GMT -5. The time now is 01:51 PM.