procmail
[Top] [All Lists]

Re: mucking with $LASTFOLDER

1997-09-19 14:48:48
Era Eriksson suggested to Timothy Luoma,

E2> TRAP="echo '
E2> FROM        $FROM
E2> TO/CC       $TO / $CC
E2> SUBJECT     $SUBJECT
E2> FOLDER      $LASTFOLDER
E2> ' | sed -e 's/^FOLDER       appnmail //"

Timothy replied,

L> Well, I want to leave the word FOLDER just remove appnmail....
L> but this doesn't work, as I just get a line
L> FOLDER
L> with nothing after it at all.... and I think the "'s" was supposed to be  
L> just "s" right?

Era followed up,

E> It worked fine here with the quoting I had originally used. I didn't
E> test this "obvious" change, regrettably, but it would actuall appear
E> that the order of the quotes is important. This is tested and works
E> for me: 

E> TRAP='echo "
E> FOLDER       $LASTFOLDER
E> LOG  $LOGFILE
E> PREFIX       $PREFIX
E> SHELL        $SHELL
E> " | sed -e "s#FOLDER /dev/#FOLDER    #g"'

Yup.

E> I'm at loss as to why the quoting would matter at all here. Can
E> anybody come up with an explanation? 

First grade sh study, Era.  Procmail evaluates expressions much the way sh
does.  Like the trap in an sh script, TRAP gets evaluated twice; once when it
is set and again when it is executed.  Consider the original:

TRAP="echo '
FROM    $FROM
TO/CC   $TO / $CC
SUBJECT $SUBJECT
FOLDER  $LASTFOLDER
' | sed -e 's/^FOLDER   appnmail //"

Inside soft quotes variables are substituted and apostrophes are treated
as ordinary characters, so $FROM, $TO, $CC, $SUBJECT, and $LASTFOLDER are
all replaced with the values that they have when TRAP is defined, which
might be different from those they'll have when the trap is executed.  In
particular, at this juncture LASTFOLDER is still unset, so TRAP is set
to this by the original code (embedded newlines are significant):

echo '
FROM    current-value-of-$FROM
TO/CC   current-value-of-$TO / current-value-of-$CC
SUBJECT current-value-of-$SUBJECT
FOLDER
' | sed 's/^FOLDER/     appnmail //

and the lack of a closing strong quote on the sed instruction doesn't
bother it, though personally I'd close it for form's sake.  When the trap
is executed, there are no variables (nor backquoted commands) to evaluate,
because they all got substituted when the trap was defined with the values
they had back then.

Now consider inverting the quotes; inside strong quotes all characters except
apostrophes are literal:

  TRAP='echo "
  FOLDER        $LASTFOLDER
  LOG   $LOGFILE
  PREFIX        $PREFIX
  SHELL $SHELL
  " | sed -e "s#FOLDER  /dev/#FOLDER    #g"'

This sets TRAP to the following (again, embedded newlines are significant):

echo "
FOLDER  $LASTFOLDER
LOG     $LOGFILE
PREFIX  $PREFIX
SHELL   $SHELL
" | sed -e "s#FOLDER    /dev/#FOLDER    #g"

where all the $-named variables are still static text until the trap is
executed; THEN they'll be evaluated, and $LASTFOLDER will be replaced
in the echo command by the value of the variable.

Using the first syntax and \$LASTFOLDER instead of $LASTFOLDER should work
as well.

<Prev in Thread] Current Thread [Next in Thread>