Danny
Danny

Reputation: 158

Procmail - passing body to bash script

I have one liner mails that I wish to send from procmail into a bash script. I only want the body to be sent, nothing else.

Currently my .procmailrc looks like this:

:0
  *^ Subject.*Xecute Command$
   {
    :0 bf
    | /bin/bash /path/to/script
   }

And my Bash script is simple:

#!/bin/bash
echo -e "\rLT 4>$0\r\r" > /dev/ttyS1

I don't get any input or output from anywhere.

Any pointers?

Upvotes: 0

Views: 1754

Answers (3)

Luca Gibelli
Luca Gibelli

Reputation: 951

formail is your friend! Pipe the message into:

:0
*^ Subject.*Xecute Command$
| formail -I "" | your-bash-script

Upvotes: 0

tripleee
tripleee

Reputation: 189628

If the intention is to add some decorations to the email message and print it to a serial port (?), try a recipe like

:0b
*^ Subject.*Xecute Command$
| ( printf '\rLT 4>'; cat -; printf '\r\r' ) > /dev/ttyS1

The b flag applies to the action line if the condition matches, so you don't need the braces and a new conditionless recipe; the f flag makes no sense at all in this context. (Though if you want to keep the message for further processing, you'll want to add a c flag.)

Also, for the record, $0 in Bash is the name of the currently running script (or bash itself, if not running a script), and $@ is the list of command-line arguments. But Procmail doesn't use either of these when it pipes a message to a script; it is simply being made available on standard input.

If you want the action in an external script, that's fine, too, of course; but a simple action like this is probably better written inline. You don't want or need to specify bash explicitly if the script file is executable and has a proper shebang; the reason to have a shebang is to make the script self-contained.

In response to comments, here is a simple Perl script to extract the first line of the first body part, and perform substitutions.

 perl -nle 'next if 1../^$/;
    s/\<foo\>/bar/g;
    print "\rLT 4>$_\r\r"; exit(0)'

This would not be hard to do in sed either, provided your sed understands \r.

Upvotes: 1

nsilent22
nsilent22

Reputation: 2863

Write your script like that:

{
echo -e "\rLT 4>"
cat
echo -e "\r\r"
} > /dev/ttyS1

Upvotes: 0

Related Questions