Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script streamlining and error-checking? 1

Status
Not open for further replies.

billieT

MIS
Dec 11, 2001
107
NZ
Hi all:

I'm a complete n00b when it comes to shell-scripting, but since I am now going to be administering a Postfix server, it's time to learn!

This script is generating some Postfix hash files from input files and then renaming the inputs once it's done. I've borrowed some error-checking from someone else's code, but I'm sure it's only checking the immediately previous command issued (rather than checking all the "mv" commands, say).

Code:
#!/bin/bash

NOW=$(date +"%F::%T")
PFX_PATH=/etc/postfix
MAPS_PATH=$PFX_PATH/map_files
POSTMAP=/usr/sbin/postmap

check_errs()
{
  # Function. Parameter 1 is the return code
  # Para. 2 is text to display on failure.
  if [ "${1}" -ne "0" ]; then
    echo "ERROR # ${1} : ${2}"
    # as a bonus, make our script exit with the right error code.
    exit ${1}
  fi
}

$POSTMAP hash:$MAPS_PATH/relay_recipients
$POSTMAP hash:$MAPS_PATH/virtual_mailbox_recipients
$POSTMAP hash:$MAPS_PATH/virtual_mailbox_aliases
$POSTMAP hash:$MAPS_PATH/transport

check_errs $? "Error with postmap"

mv $MAPS_PATH/relay_recipients.db $PFX_PATH
mv $MAPS_PATH/virtual_mailbox_recipients.db $PFX_PATH
mv $MAPS_PATH/virtual_mailbox_aliases.db $PFX_PATH
mv $MAPS_PATH/transport.db $PFX_PATH

check_errs $? "Error with map move"

mv $MAPS_PATH/relay_recipients $MAPS_PATH/relay_recipients.$NOW
mv $MAPS_PATH/virtual_mailbox_recipients $MAPS_PATH/virtual_mailbox_recipients.$NOW
mv $MAPS_PATH/virtual_mailbox_aliases $MAPS_PATH/virtual_mailbox_aliases.$NOW
mv $MAPS_PATH/transport $MAPS_PATH/transport.$NOW

check_errs $? "Error with input file rename"

/usr/sbin/postfix reload

Is there a convenient way of error-checking each command and/or condensing the commands so that I'm not writing 4 nearly-identical lines for each instance? I'm sure a "for" loop would do something here, but I think it'd be easy for me to get confused. The code works at present, despite its cumbersomeness.
 
Something like this?

Code:
#!/bin/bash

NOW=$(date +"%F::%T")
PFX_PATH=/etc/postfix
MAPS_PATH=$PFX_PATH/map_files
POSTMAP=/usr/sbin/postmap
LIST="relay_recipients virtual_mailbox_recipients virtual_mailbox_aliases transport"


check_errs()
{
  # Function. Parameter 1 is the return code
  # Para. 2 is text to display on failure.
  if [ "${1}" -ne "0" ]; then
    echo "ERROR # ${1} : ${2}"
    # as a bonus, make our script exit with the right error code.
    exit ${1}
  fi
}

for file in ${LIST}
do
  $POSTMAP hash:$MAPS_PATH/${file}
  check_errs $? "Error with postmap ${file}"
done

for file in ${LIST}
do
  mv $MAPS_PATH/${file}.db $PFX_PATH
  check_errs $? "Error with ${file} map move"
done

for file in ${LIST}
do
  mv $MAPS_PATH/${file} $MAPS_PATH/${file}.$NOW
  check_errs $? "Error with input ${file} rename"
done

/usr/sbin/postfix reload

You get a check of every operation, not just the last operation in each block of 4,; And there's just 3 easy to read for loops, all using the same list of file base names...


HTH,

p5wizard
 
That's awesome, and exactly what I vaguely had in mind for the needed structure.

Thank you so much for making it look so simple and clear! :-D
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top