On Tue, Mar 31, 2009 at 10:59:41AM -0700, Bryce Nesbitt wrote:

> Here is try two at the end of line comment script for /etc/aliases.  Can
> anyone do this more elegantly?
> 
> #!/bin/sh
> # Created so we can have end of line comments in /etc/aliases
> sed 's/#.*$//;/^$/d' < /etc/aliases > /tmp/aliases_tmp ;
> /usr/sbin/sendmail -bi -oA/tmp/aliases_tmp ; mv /tmp/aliases_tmp.db
> /etc/aliases.db

Only if /etc and /tmp are in the same file-system. Only if you don't
use a fixed predictable temporary file name. Much better to do the the
work in the directory containing the aliases file, and do it robustly
(on systems that have mktemp(1)). Something along the lines of:

    shift $#; set -- $(postconf -h alias_database)
    if [ $# -eq 1 ]; then
        mappath=$(echo "$1" | sed -e 's/^[^:]*://')
        case "$mappath" in /*) ;; *) exit 1;; esac
        test -f "$mappath" || exit 1
        mapdir=$(dirname "$mappath")
        mapfile=$(basename "$mappath")
        test -n "$mapfile" || exit 1
        cd "$mapdir" || exit 1
        find . ! -name . -type d -prune -o \
            -name ".$mapfile.??????" -mtime +1 -exec rm "{}" ";"
        maptmp=$(mktemp ".$mapfile.XXXXXX") || exit 1
        rm -f "${maptmp}".*
        cp -p "$mappath" "$maptmp"      # preserve file owner/permissions
        sed 's/#.*$//;/^$/d' < "$mapfile" > "$maptmp"
        postalias "$maptmp"
        test -f "$maptmp".db && mv $maptmp.db "$mapfile.db"
        rm -f "$maptmp".*
        rm "$maptmp"
    fi

-- 
        Viktor.

Disclaimer: off-list followups get on-list replies or get ignored.
Please do not ignore the "Reply-To" header.

To unsubscribe from the postfix-users list, visit
http://www.postfix.org/lists.html or click the link below:
<mailto:majord...@postfix.org?body=unsubscribe%20postfix-users>

If my response solves your problem, the best way to thank me is to not
send an "it worked, thanks" follow-up. If you must respond, please put
"It worked, thanks" in the "Subject" so I can delete these quickly.

Reply via email to