Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

when i try to execute another scripts if fails

by john.tm (Beadle)
on Jun 17, 2014 at 21:19 UTC ( #1090212=perlquestion: print w/ replies, xml ) Need Help??
john.tm has asked for the wisdom of the Perl Monks concerning the following question:

i am trying to call another script from within a script, which opens up an outlook email if a file is empty,This all works fine. i have tested it in other scripts.
unless ( -z "$Temp" ) { print "File has something in it\n"; exec "c://testscripts//email.pl" or die "problem with email.pl"; }
But it will not work in this script, i get no warnings.
#!/usr/local/bin/perl use warnings; use strict; use POSIX 'mktime'; use POSIX 'strftime'; my @now = localtime(); my $todaysday = strftime("%d" , localtime()); my $thismonth = strftime("%b" , localtime()); my $year = strftime("%Y" , localtime()); my $SERVICEDESK="servicedesk"; my $CLIENTFILE = strftime("%Y_backup_report.csv" , localtime()); my $Temp = strftime("C:\\temp\\Unknown_servers%d%b.txt" , localtime()) +; my $already_there = 0; open my $mon, '<', "c:\\DBR_$year\\$thismonth\\Monthly_backup_report\\ +$thismonth$CLIENTFILE" or die "read file $!\n"; while ( defined ( my $line = <$mon> ) ) { my ( $date ) = split ',', $line; if ( $date eq "$todaysday $thismonth $year" ) { print "\ file has already been updated today"; $already_there++; last; } } close $mon; if ( ! $already_there ) { open my $day, "c:\\DBR_$year\\$thismonth\\Failures_output\\$todays +day$thismonth$SERVICEDESK.csv" or die " 22read file $!\n"; open $mon, '>>', "c:\\DBR_$year\\$thismonth\\Monthly_backup_report +\\$thismonth$CLIENTFILE" or die "33 write to file $!\n"; <$day>; # get past header while ( <$day> ) { print { $mon } $_; print " $thismonth$CLIENTFILE\n"; print " Has also been updated with today's failures \ +n"; print " Please update the Resolutions when they are received + \n\n"; print " ********************************************************** +***\n"; } close $day or die "Could not complete writing to monthly file: $!\ +n"; close $mon or die "Could not complete writing to monthly file: $!\ +n"; } exit $already_there; ### call email perl #################### unless ( -z "$Temp" ) { print "File has something in it\n"; exec "c://testscripts//email.pl" or die "problem with email.pl"; } #########################################

Comment on when i try to execute another scripts if fails
Select or Download Code
Re: when i try to execute another scripts if fails
by AnomalousMonk (Abbot) on Jun 17, 2014 at 21:33 UTC
    exit $already_there; ### call email perl #################### unless ( -z "$Temp" ) { print "File has something in it\n"; exec "c://testscripts//email.pl" or die "problem with email.pl"; }

    If the intention is that the  unless (cond) { ... } block following the exit statement be executed, it will not and cannot be: the script has exit-ed! Did you mean to write
        exit if $already_there;
    (a conditional statement) perhaps?

Re: when i try to execute another scripts if fails
by 2teez (Priest) on Jun 17, 2014 at 21:40 UTC

    But it will not work in this script, i get no warnings.

    Let me ask: Will this code below display the second print?

    #!/usr/bin/perl -l use warnings; use strict; my $value = 0; print 'home'; exit $value; ## Note here please print 'another home';
    If not! Why? That would be the answer :)

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: when i try to execute another scripts if fails
by Anonymous Monk on Jun 17, 2014 at 23:53 UTC
    Remember Re: check to see if a file has todays data in it, if it has exit if not then add data? Why don't you write code that way?
    #!/usr/bin/perl -- use strict; use warnings; use POSIX qw/ strftime mktime /; Main( @ARGV ); exit( 0 ); sub Main { ... if( FileUpdatedToday( $filename ) ){ print "\ file has already been updated today"; } else { UpdateFile( $filename ); } if( not -s $temp ){ EmailReport( $temp ); } } sub EmailReport { my( $temp ) = @_; exec $^X, $emailPL, or die "problem with $emailPL"; } sub FileUpdatedToday { ... return !!1; .. return !!0; } sub UpdateFile { ... }

    Its easier this way to figure out your logic, to reason about when/what should happen, and you can even write simple tests to make sure each part works the way you want

    sub TestFileUpdatedToday { require Test::More; Test::More->import( tests => 4 ); PrepareTestfiles('testfiles'); my $notexist = 'testfiles/notexist'; my $empty = 'testfiles/empty'; my $stale = 'testfiles/stale'; my $updated = 'testfiles/updated'; for my $file ( $notexist , $empty, $stale ){ ok( ! FileUpdatedToday( $file ), "FileUpdatedToday( $file ) sh +ould not be true"); } ok( FileUpdatedToday( $updated ), "FileUpdatedToday( $updated ) sh +ould be true"); }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1090212]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (16)
As of 2015-07-03 08:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (49 votes), past polls