help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] safty of my script


From: Dan Douglas
Subject: Re: [Help-bash] safty of my script
Date: Sun, 09 Mar 2014 11:20:48 -0500
User-agent: KMail/4.12.3 (Linux/3.13.2-pf+; KDE/4.12.3; x86_64; ; )

On Sunday, March 09, 2014 01:21:35 PM Martin wrote:
> Hi there,
> 
> Looking at the web, there is no possibilty to combine offlineimap with
> spamassassin.  So I wrote a script, and now I'd like to know about your
> experience, how I could make it more safe if there is any problem with
> offlineimap or spamc (from spamassasin) not to loose emails.  Here is my
> first version so far (which I don't really use, I'm a little afraid of
> loosing emails.):
> 
> 
> ---- start ~/bin/email_sync.sh ----
> #!/bin/bash
> 
> cd ~/Maildir
> touch .before_start_offlineimap
> offlineimap
> 
> files=$(find . -type f -newer .before_start_offlineimap  | grep new)
> 
> for f in ${files}
> do
>     t=$(mktemp -t email-sync-XXXXXXXXXXXXXX)
>     mv "${f}" "${t}"
>     spamc < "${t}" > "${f}"
>     rm "${t}"
> done
> 
> rm .before_start_offlineimap
> ---- end ----

Hi, There are probably better places to ask for help with "offlineimap" and
"spamassassin". Ignoring what those actually do, I can address the
scripting issues. See comments.

        function main {
                cd ~/Maildir || return 1

                typeset \
                        startTime \
                        findPrecision=s \
                        tmp=$(mktemp -qt email-sync-XXXXXXXXXXXXXX) # We 
overwrite this on each iteration anyway

                # This block is overkill.
                if (( BASH_VERSINFO[0] >= 4 && BASH_VERSINFO[1] >= 2 )); then # 
newer bash
                        printf -v startTime '%(%s)T' -1
                        shopt -s lastpipe
                elif [[ ${!KSH_VERSION} == .sh.version ]]; then               # 
ksh93
                        startTime=$(printf '%(%.s)T' now)
                        findPrecision=@
                elif ! ${EPOCHREALTIME+false}; then
                        if ! ${ZSH_VERSION+false}; then                         
  # zsh
                                emulate ksh
                                startTime=$EPOCHREALTIME
                                findPrecision=@
                        else                                                    
  # mksh
                                startTime=${EPOCHREALTIME%.*}
                        fi
                else                                                          # 
older bash/ksh
                        startTime=$(date +%s)
                fi

                sleep 1 # wait for the less precise shells
                offlineimap

                # The biggest problem with the old code is unsafe filename 
handling.
                # You should be using null-delimited elements whenever cramming 
filenames
                # into a stream and then parsing them out again.
                # http://mywiki.wooledge.org/UsingFind
                # http://mywiki.wooledge.org/BashFAQ/003
                # http://mywiki.wooledge.org/BashPitfalls#pf1

                find . -type f -name '*new*' -printf "%T${findPrecision}\0%p\0" 
|
                        while IFS= read -rd '' mTime && IFS= read -rd '' 
fileName; do
                                (( mTime <= startTime )) && continue 
                                mv -f -- "$fileName" "$tmp" 
                                spamc <"$tmp" >"$fileName" 
                done

                rm -f -- "$tmp"
        }

        main "$@"

-- 
Dan Douglas



reply via email to

[Prev in Thread] Current Thread [Next in Thread]