[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] safty of my script
From: |
Martin |
Subject: |
Re: [Help-bash] safty of my script |
Date: |
Sun, 09 Mar 2014 19:45:42 +0100 |
User-agent: |
mu4e 0.9.9.5; emacs 24.3.1 |
Thanks, but I have a few questions about it:
Dan Douglas writes:
> 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.
Ok, I just use that, maybe that is more for compatibility on different
systems...
> 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
About this block above, for me it seems not to work.
1)
find . -type f -name "*new*"
seems not to be the same like
find . -type f | grep new
because here i get also files which are inside the new directory of the
Maildir folders:
~/Maildir$ find . -type f | grep new
./kleinerdrache/INBOX/new/1394389783_1.14775.martin-desktop,U=1501,FMD5=7e33429f656f1e6e9d79b29c3f82c57e:2,
but find . -type f -name "*new*" does not find anything.
2)
(( mTime <= startTime )) -> this is what I wanted to do already with
find, but maybe this doesn't matter because there arent so much
files if we filter out the new directories (there are 68 directories
with the name 'new' in the Maildir subdirectories here where the run of
offlineimap may create most of the time 0 new files, but maybe sometimes
up to about 50 new files).
3)
I'm not very good in bash programming, but I'd like to be safe if the mv
or the spamc failes. So maybe the mv could be a cp ? and if spamc
failes for some reason (or even does not exist, or ...) I would like to
copy the tmp file back... or is my thought bad, maybe I missunderstand
the issue?
Thank you very much for your first hint to me.
Martin
> rm -f -- "$tmp"
> }
>
> main "$@"