procmail
[Top] [All Lists]

procmail / sed / echo / shells - quoting issues

2000-04-28 20:57:19
I just spent a while finding out why certain constructs didn't work
properly. It turned out to be a quoting problem, various times caused
by procmail, the shell, the echo command, and sed.

Could this perhaps please included in the FAQ / mini-FAQ / Jari's tips
page whereeverelseuseful - thanks.

Following a test (save the first part to mmm, the last to pro).

Volker

----mmm----
From email(_at_)host Tue Apr  4 13:52:54 2000
subject: AAA- \ -Z\nZZ

From email(_at_)host Tue Apr  4 13:52:54 2000
subject: AAA- / -Z\nZZ

From email(_at_)host Tue Apr  4 13:52:54 2000
subject: AAA- \/ -Z\nZZ

From email(_at_)host Tue Apr  4 13:52:54 2000
subject: AAA- /\ -Z\nZZ

From email(_at_)host Tue Apr  4 13:52:54 2000
subject: AAA- \\ -Z\nZZ

From email(_at_)host Tue Apr  4 13:52:54 2000
subject: AAA- \/\/ \\\ \\\\ \\\\\ // /// \/\// \/// \\/\/\/ -Z\nZZ

From email(_at_)host Tue Apr  4 13:52:54 2000
subject: AAA- \\/\\/\/ \\/\\/\\/ \\\/\\/\\/ \\\/\\\/\\/ \\\/\\\/\\\/ -Z\nZZ

From email(_at_)host Tue Apr  4 13:52:54 2000
subject: AAA- $ \$ \\$ $$ -Z\nZZ

----mmm----
----pro----
# Running the contents of a variable through sed - quoting issues.
# Useful for replacing a literal '$SUBJECT' with the actual subject of
# the current message.
#
# run this with:  formail -dbs procmail ./pro < mmm
# examine output with:  grep subj out  (each 2 lines identical)
#
# Volker Kuhlmann, 29 Apr 2000

SHELL=/bin/sh
#SHELL=/bin/ksh
#SHELL=/usr/bin/bash
VERBOSE="on"
LOGABSTRACT="all"
LOGFILE=./log

# WARNING: This must be an echo which does not interpret any backslash
# escapes in its (quoted) argument.
# Solaris 2.7:  /usr/ucb/echo is ok, /bin/echo and the built-in echos of 
#   /bin/sh, /bin/ksh are not. Built-in echos of /bin/csh, /bin/tcsh are ok.
# Linux (SuSE 6.3):  /bin/echo is ok, likewise built-ins of bash, csh, tcsh.
#ECHO="echo"
ECHO="/usr/ucb/echo"

:0      # subject in MATCH (init to "" in case there is no Subject:;
        #   also ensures recipe is always executed)
* 1^0 .*$\/
* 1^0 ^Subject:[        ]*\/[^  ].*
{ }

# The "S=" must be quoted with doublequotes to preserve space, backslash,
# probably others.
# Inside doublequotes, backslashes must be doubled for procmail.
# For sed, a literal backslash "\" can be obtained by "\\".
# For the second sed (the one replacing $SUBJECT) we must escape the
# characters "\&/"; "\&" because they have special meaning for sed, and "/"
# because it's used as RE delimiter.
# Makes a lot of backslashes in places...  but wouldn't work with any less.
:0 bfw
| S="`$ECHO \"$MATCH\" | tee -a s \
    | sed -e 's,\\\\,\\\\\\\\,g' -e 's,/,\\\\/,g' -e 's/&/\\\\&/g' \
    | tee -a s`"; \
  $ECHO "$S" >>s; echo "---" >>s; \
  echo 'subject: $SUBJECT' | sed -e "s/\$SUBJECT/$S/g"

:0:
./out
----pro----

<Prev in Thread] Current Thread [Next in Thread>
  • procmail / sed / echo / shells - quoting issues, Volker Kuhlmann <=