Igor Chudov wrote:
> #!/bin/bash

Since there are no bash specific features this could be a standard
/bin/sh just as easily and then does not depend upon bash.

> PM=`perl -MConfig -e 'print 
> "$Config{installsitelib}"'`/Mail/SpamAssassin/Plugin/PDFInfo.pm
> CF=/etc/mail/spamassassin/PDFInfo.cf
> 
> cp $PM $PM.bak || exit 1 # Probably I am not root...
> cp $CF $CF.bak || exit 1 # same

Instead of backing up and removing these later I think it is better to
download the files into a temporary location and if valid then move
them into position.

> echo Downloading, veryfying perl module and size of config file...
> 
> if wget -q -O $PM http://www.rulesemporium.com/plugins/PDFInfo.pm             
>         \
>     && wget -q -O $CF http://www.rulesemporium.com/plugins/pdfinfo.cf         
>         \
>     && perl -MMail::SpamAssassin::Plugin::PDFInfo -e print "Perl Module 
> PDFInfo OK\n" \
>     && test -s $CF ; then
>   echo Successfully downloaded $PM and $CF:
>   chmod 644 $PM $CF

There is an order of events problem as this stands.  If the first wget
succeeds but the second wget or later commands fail then the chmod
never happens.  If the chmod is needed (I don't think it should be)
then the first file is left in a bad state.  If it is not needed then
this is simply redundant here.

>   ls -l $PM $CF

Of course that is simply left over from debugging.

>   rm $CF.bak $PM.bak

This should be done in a shell EXIT trap so that it always happens.

>   echo Restarting SpamAssassin:
>   service spamassassin restart

Hmm...  spamassassin?  Or spamc?  I think your system is different
from mine.  Best to double check.

>   exit 0
> else
>   echo FAILED to download $PM and $CF
>   mv $CF.bak $CF
>   mv $PM.bak $PM
>   exit 1
> fi

This is untested and just typed in here off the top of my head but let
me suggest some improvements.  Among other things the trap handling
here means that temporary files will not be left behind even if the
script is interrupted and errors are reported for each type of
possible error.

#!/bin/sh

PM=`perl -MConfig -e 'print 
"$Config{installsitelib}"'`/Mail/SpamAssassin/Plugin/PDFInfo.pm
CF=/etc/mail/spamassassin/PDFInfo.cf

trap 'rm -f $PMTMP $CFTMP' EXIT
PMTMP=$(mktemp -t pdfinfo.pm.XXXXXXXX) || exit 1
CFTMP=$(mktemp -t pdfinfo.cf.XXXXXXXX) || exit 1
chmod a+r $PMTMP $CFTMP

echo Downloading, veryfying perl module and size of config file...

if ! wget -q -O $PMTMP http://www.rulesemporium.com/plugins/PDFInfo.pm; then
  echo FAILED to download http://www.rulesemporium.com/plugins/PDFInfo.pm
  exit 1
fi
if ! wget -q -O $CFTMP http://www.rulesemporium.com/plugins/pdfinfo.cf; then
  echo FAILED to download http://www.rulesemporium.com/plugins/pdfinfo.cf
  exit 1
fi
if ! test -s $PMTMP ; then
  echo ERROR the downloaded PDFInfo.pm file is zero sized
  exit 1
fi
if ! test -s $CFTMP ; then
  echo ERROR the downloaded pdfinfo.cf file is zero sized
  exit 1
fi
if ! perl -cw $PMTMP; then
  echo FAILED syntax check of new PDFInfo.pm module
  exit 1
fi

echo Successfully downloaded $PM and $CF, installing:

mv $CFTMP $CF || exit 1 # Probably I am not root...
mv $PMTMP $PM || exit 1 # same

echo Restarting SpamAssassin:
service spamassassin restart

exit 0

Reply via email to