Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 08 Apr 2007 09:35:10 -0700
From:      Garrett Cooper <youshi10@u.washington.edu>
To:        freebsd-questions@freebsd.org
Subject:   Re: perl/script and retval
Message-ID:  <461919BE.7080903@u.washington.edu>
In-Reply-To: <46190C84.4080907@steelbox.org>
References:  <46190C84.4080907@steelbox.org>

next in thread | previous in thread | raw e-mail | index | archive | help
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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?461919BE.7080903>