perl/script and retval

Giorgos Keramidas keramida at ceid.upatras.gr
Sun Apr 8 18:07:46 UTC 2007


On 2007-04-08 17:38, Olivier Regnier <oregnier at 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



More information about the freebsd-questions mailing list