From owner-freebsd-questions@FreeBSD.ORG Sun Apr 8 18:07:46 2007 Return-Path: X-Original-To: freebsd-questions@freebsd.org Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [69.147.83.52]) by hub.freebsd.org (Postfix) with ESMTP id DB08C16A401 for ; Sun, 8 Apr 2007 18:07:46 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from igloo.linux.gr (igloo.linux.gr [62.1.205.36]) by mx1.freebsd.org (Postfix) with ESMTP id 5514613C4C5 for ; Sun, 8 Apr 2007 18:07:45 +0000 (UTC) (envelope-from keramida@ceid.upatras.gr) Received: from kobe.laptop (dialup5.ach.sch.gr [81.186.70.5]) (authenticated bits=128) by igloo.linux.gr (8.13.8/8.13.8/Debian-3) with ESMTP id l38I76l5032067 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=NOT); Sun, 8 Apr 2007 21:07:19 +0300 Received: from kobe.laptop (kobe.laptop [127.0.0.1]) by kobe.laptop (8.13.8/8.13.8) with ESMTP id l38I75NB001875; Sun, 8 Apr 2007 21:07:05 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Received: (from keramida@localhost) by kobe.laptop (8.13.8/8.13.8/Submit) id l38I73nI001874; Sun, 8 Apr 2007 21:07:03 +0300 (EEST) (envelope-from keramida@ceid.upatras.gr) Date: Sun, 8 Apr 2007 21:07:02 +0300 From: Giorgos Keramidas To: Olivier Regnier Message-ID: <20070408180702.GC1672@kobe.laptop> References: <46190C84.4080907@steelbox.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <46190C84.4080907@steelbox.org> X-Hellug-MailScanner: Found to be clean X-Hellug-MailScanner-SpamCheck: not spam, SpamAssassin (not cached, score=-3.69, required 5, autolearn=not spam, ALL_TRUSTED -1.80, AWL 0.51, BAYES_00 -2.60, DNS_FROM_RFC_ABUSE 0.20) X-Hellug-MailScanner-From: keramida@ceid.upatras.gr X-Spam-Status: No Cc: freebsd-questions@freebsd.org Subject: Re: perl/script and retval X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 08 Apr 2007 18:07:46 -0000 On 2007-04-08 17:38, 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 This script has a minor bug. There is no != test for numeric values in sh(1) scripts. See below for a slightly larger shell script with some reusable parts, which works probably better. > 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. Missing quotes at the end of the system("... line? In this case, it's quite easy to run the command in a simple shell script. WHy do you *have* to rewrite everything in Perl? If it's because you want to learn how something like this could be done in Perl too, that's ok. But it the reason is because "Perl is faster", "Perl is safer", or some other similar 'optimization' related reason, then just don't :) A small shell script can go very far along the way, i.e.: #!/bin/sh msg() { echo "===> $*" } err() { retval=$1 shift echo >&2 "ERROR: $*" exit $retval } timestamp() { date '+%Y-%m-%d %H:%M:%S' } get_doc_files() { doc_supfile="$1" if [ -z "${doc_supfile}" ]; then err 1 "no supfile for 'doc' collection" fi msg "Download of 'doc' collection started at" `timestamp` /usr/bin/csup "$doc_supfile" msg "Download of 'doc' collection finished at" `timestamp` } get_doc_files /root/supfiles/doc-supfile if [ $? -ne 0 ]; then err 1 "get_doc_files failed." fi