Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 8 Apr 2007 21:07:02 +0300
From:      Giorgos Keramidas <keramida@ceid.upatras.gr>
To:        Olivier Regnier <oregnier@steelbox.org>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: perl/script and retval
Message-ID:  <20070408180702.GC1672@kobe.laptop>
In-Reply-To: <46190C84.4080907@steelbox.org>
References:  <46190C84.4080907@steelbox.org>

next in thread | previous in thread | raw e-mail | index | archive | help
On 2007-04-08 17:38, Olivier Regnier <oregnier@steelbox.org> 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




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