TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG:8979] perl question



Hello Carl:

> From: "Carl Wilhelm Soderstrom" <carls@agritech.com>
> Date: Tue, 05 Oct 1999 14:18:09 -0700
> Subject: [TCLUG:8979] perl question
>
>         hate to ask more tiresome questions, but...
>         I wrote a trial script, and it seems to work. However, I would like
> it to be more robust (because unexpected failures could be very bad in this
> case).
>         I need to make sure that a given SMB share is mounted, before I do a
> backup. (I've had the share not respond to the mount; and done backups on
> empty space).
>         I'm guessing that the 'die' option will let me exit gracefully from
> a failed command; that seems to be what it's there for. (can't find it
> readily explained in the man pages anywhere, only examples of its use; nor
> does perl.com's search engine provide me with help)

Check out the CPAN site:

    http://www.perl.com/CPAN/README.html

Specifically:

    http://www.perl.com/CPAN/doc/manual/html/pod/perlfunc/die.html

Or look in the 'perlfunc' man page.

>         however, with this script:
> #!/usr/bin/perl
> if (-e "/mnt/e/HERDS") {
>         print "/mnt/e/HERDS exists! \n";
>         `/usr/bin/smbumount /mnt/e` or die "Unmount failed!";
>         }
> else {
>         print "/mnt/e/HERDS does not exist! \n";
>         `/usr/bin/smbmount //nonexistent/share -c "mount /mnt/e"` or die
> "Mount failed!";
>         }
>
> it unmounts the share in the first case; but still gives me the "Unmount
> failed" error.
> in the second case, it doesn't mount the share; but doesn't give me the
> "Mount failed" error.
>
> is the execution of the shell command returning an incorrect code, compared
> to what 'die' is looking for?
> or am I barking up the wrong tree here?

You are barking up the right tree.  :-)

Standard Perl syntax like:

    open(FH,'foo') or die "Can't open foo: $!";

uses the "shortcut" OR, where the 'die' only gets evaluated if
open() returns false (0).  If you have code like

    `smbmount x` or die "Can't smbmount";

You're depending on the shell command returning 0 for failure, which
is probably wrong.  Maybe something like:

#!/usr/bin/perl

if (-e "/mnt/e/HERDS") {
   print "/mnt/e/HERDS exists! \n";
   `/usr/bin/smbumount /mnt/e`;
   my $exit_value = $? >> 8;
   ($exit_value == 0) or die "Unmount failed!";
} else {
   print "/mnt/e/HERDS does not exist! \n";
   `/usr/bin/smbmount //nonexistent/share -c "mount /mnt/e"`;
   my $exit_value = $? >> 8;
   ($exit_value == 0) or die "Mount failed!";
}

> do people have any advice for making scripts more robust?

Check out

    http://www.perl.com/CPAN/doc/manual/html/pod/perlfunc/system.html

for more details on "the right thing".

John