perl/script and retval

Garrett Cooper youshi10 at u.washington.edu
Sun Apr 8 16:35:17 UTC 2007


Olivier Regnier wrote:
> Hello,
> 
> I written a small script in sh :
> # Downloading doc files
> echo "===> Downloading doc files"
> /usr/bin/csup $doc_supfile
> RETVAL=$?
> if [ $RETVAL != 0 ]; then
>    echo "abort"
>    exit 0
> fi
> 
> I want to rewritte this code in perl script.
> 
> my $retval=0;
> my $doc_supfile="/etc/doc-supfile";
> 
> # Downloading doc files
> print "===> Downloading doc files\n";
> system("/usr/bin/csup $doc_supfile
> if (! $retval) {
> print "abort";
> exit;
> }
> I don't know what happened with retval but that doesn't work correctly.
> 
> Can you help me please ?
> 
> Thank you :)

Olivier,
	Why are you doing this all in perl? Doesn't Bourne shell suffice :)?

Try:

====================================
#!/usr/local/bin/perl -w

use strict;

local $?=0;

my $doc_supfile="/etc/doc-supfile";
print "===> Downloading doc files\n";
system "/usr/bin/csup $doc_supfile";

my $retval = $?; # make sure to grab exit val right after execution;
		 # this can shoot you in the foot if you do it later on
		 # down the line, and another command has been executed
		 # behind the scenes.. Also read perldoc -f system for
		 # more details on return codes because $retval doesn't
		 # necessarily match the exit code that your shell may
		 # see.

unless($retval) {
	print "CVsup aborted\n";
	exit $retval;
}
====================================

See perldoc perlvar.

Cheers,
-Garrett


More information about the freebsd-questions mailing list