bash pipe construct to prepend something to the stdoutput of previous command
I want to use sendmail to send me stuff and want to do it in a oneliner.
echo "mail content" | sendmail emailataddres.com
Sends it without subject.
The subject line must come before the Mail content, so I am looking for something along the lines of:
echo "mail content" | prepend "Subject: All that matters" | sendmail emailataddres.com
sed and awk tend to be really awkward to use and remember.
EDIT:Just to clarify: echo "Mail content" is just an illustrating example. I need to be able to prepend stuff to stdout streams from any source. e.g.: ifconfig, zcat, etc..
$ echo 1 | (echo 2 && cat)
2
1
I am pretty sure that there is a nicer solution, but this should do.
Either use what Claudius said or make your own:
~/bin/prepend:
#!/bin/sh
echo -en "$@"
cat -
e.g.:
$ echo "Splendid SUPANINJA! Let's do it!" |\
prepend "Subject: Venetian Snares\n"
Subject: Venetian Snares
Splendid SUPANINJA! Lets do it!
This is inspired by Claudius answer.
If you don't want a break return between your outputs, add the -n
param. This will look like:
$ echo 1 | (echo -n 2 && cat)
Which will give:
21
From the pieces I've gathered... you could do something like this:
echo "Subject: All that matters
`echo "mail content"`" | sendmail blah@blahblah
Notice that I did not close the quotes on the first line... because not all shells translate the \n
into a newline character... but I have yet to find one that wont process an actual newline inside the quotes.
When a command is enclosed in the ` character, it will be executed and the output will be injected in-place. Keep in mind that this bit of code is a bit dangerous as it is possible to inject additional commands inline that could easily compromise your system...
****edit**** Following advise of Claudius, a cleaner option would look like this:
echo -e "Subject: All that matters \n $(echo "mail content") |sendmail blah@blahblah
Even with that template, it could be exploited.
You can prepend to piped output using process substitution:
$ echo "mail content" | cat <(echo "Subject: All that matters") -
Subject: All that matters
mail content