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

Re: (v) ugly (and simple) bash script...



On Sat, May 21, 2022 at 10:08:42AM +0100, Morgan Read wrote:
> I've come up with:
> `cd ~/Maildir/new/; for f in *; do mv -- "$f" ~/Maildir/cur/"${f%}.eml"; cd
> -; done`

You aren't checking whether the first cd succeeds.  If it fails for any
reason, you're going to end up moving file(s) out of wherever you happen
to be at the time.

You also don't want that "cd -" to be *inside* the loop.  If there's more
than one file in ~/Maildir/new/, you're going to move the first one, then
cd back to where you were, then try to move the second file by its
relative name from the directory you changed *back* to.  There probably
won't be a file by that name in the original directory, so it'll probably
give an error... but if it doesn't, then you've screwed up big time.

If this is a script, you don't need the "cd -" at all.  Just let the
script exit, and it won't matter what directory it ended in.

Finally, ${f%} is just a fancy way of writing $f.  You're literally saying
"take the value of $f but remove the empty string from the end of it".

#!/bin/sh
cd ~/Maildir/new/ || exit 1
for f in *; do
  mv -- "$f" ../cur/"$f.eml"
done

> Which perhaps does the job...

I must admit I do not understand what the "job" actually is, or why
you're doing this.

> Getting postfix to run that command on mail delivery is the next question?

Now that looks like something closer to an actual goal statement.  I
still don't understand why you want messages to be marked as "not new"
and given a different filename, but presumably there is some rationale
for it.

I don't know postfix, but presumably what you want to do here is find
whatever mechanisms or hooks it has for performing local mail deliveries.
Arrange for your script to be executed after a successful delivery to
~/Maildir/.

In qmail, local delivery is controlled by the user's ~/.qmail file
(or ~/.qmail-* for the user's personal address space).  Inside the ~/.qmail
file, a local delivery to a Maildir is indicated by a line beginning
with . or / and ending with / .  A program to be executed is indicated by
a line beginning with | .

So, to do this in qmail, you could create a ~/.qmail file containing
something like this:

./Maildir/
|./bin/myscript

"myscript" will be fed the content of the email being delivered on stdin,
but you can simply ignore that.

I'm guessing postfix has something similar to this.  You'll have to read
up on it yourself.  Pay close attention to how postfix interprets the
exit status of the programs that you run, and make sure you exit with
an appropriate status.  For example, in the example script I gave above,
if the cd fails, I told it to exit with status 1.  But status 1 is not
explicitly defined by qmail-command(8), so another exit status might
be more appropriate.  I also didn't check whether the mv succeeds.  You
might want to do that, and abort with a specific exit status if any
mv command fails.


Reply to: