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).
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.
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.