Hi

On 17/10/06, Sonia Hamilton <[EMAIL PROTECTED]> wrote:
I've written a small script that archives email - it works, but I was
wondering if there's any better way to write it (apart from using
another language).
Damn.

The script is:

> for i in z_bak:7 root:14 y_spam_definite:56 ; do
>     mydir=${i%:*}
>     mydays=${i#*:}

$mydir is the directory to cleanup, $mydays is the # of days I want to
keep email. Is there any better way of writing the for loop to go thru
the 2 sets of values?

Not that I can see - mildly curious to know if there is.  AFAIK Bash
only has 1-dimensional arrays (in my version), certainly no hashes.
The above script looks quite compact along with those annoying
substitution operators that I can never remember.

I tend to use 'case' if I'm shelling - which is far more verbose:

for mydir in z_bak root y_spam_definite ; do
 case $mydir in
   z_bak)
     mydays=7
   ;;
   foo|bar|baz)
     mydays=100
   ;;
   *)
      echo "$0: Bad mail directory used : '$mydir'" >&2
   ;;
 esac
done

----

Here is a crazy version using arrays:

expires=(
z_bak:7
root:14
y_spam_definite:56
)

for (( i=0; i<${#expires[*]}; i++ )); do
 j=${expires[$i]}
 mydir=${j%:*}
 mydays=${j#*:}
done

It's kinda lost some clarity at this point (!).  What I was trying to
do was to put the delete settings at the top in a simple list for easy
configuration.
(I've used arrays before in a script which stores my sometimes
numerous working directories in a file so that I can call these up and
jump back and forth between locations more easily)

Daniel.
--
SLUG - Sydney Linux User's Group Mailing List - http://slug.org.au/
Subscription info and FAQs: http://slug.org.au/faq/mailinglists.html

Reply via email to