On 27 Aug, Robert Brown wrote:
| This is probably an FAQ, but I cannot find the answer.
| Apparently, =| embeds any newlines that occur in the command into the
| variable. For example,
| FOO=|echo 4 \* 5 | bc
| lets FOO have a newline as the last character in it.
| The question is: how can I chop/strip/delete/remove/get rid of the newline
| before I use it later?
| For example,
| BAR=|sed -e 's/XXX/'$FOO'/'
| later in the script will fail due to the embedded new-line.
| (I cannot use `command` (backticks) because I need to check the return
| status of the command.)
| Bash is my shell. Perhaps there's a bash way to do this?
| Please cc me on any responses since I no longer subscribe to this list.
The premise is incorrect. The =| construct is not embedding newlines. I
thought maybe it would be true of a line oriented tool like sed, but a
couple quick tests disprove that. The only way I get a newline is by
explicitly adding it. Running a test message through the following
VAR1=| formail -zx X-Loop:
VAR2=| echo 123
VAR3=| echo $VAR1 |sed 's,\.com,.net,'
VAR4=| echo -e "$VAR1\n" |sed 's,\.com,.net,'
VAR5=| wc -c
FOO=|echo 4 \* 5 | bc
BAZ=|echo $BAR |sed -e 's/x/'$FOO'/'
LOG="VAR1=$VAR1; VAR2=$VAR2; VAR3=$VAR3;$NL"
LOG="FOO=$FOO; BAR=$BAR; BAZ=$BAZ;$NL"
Produces the following output:
; VAR5= 2415;
FOO=20; BAR=number x; BAZ=number 20;
You can see the only newline embedded in a variable is the one I
specifically put into VAR4. Even with plain old echo, which does append
a newline, procmail strips it from the variable assignment.
This is only a guess, but are you tring to modify and assign to BAR in
one fell swoop? In other words, are you expecting the existing value
of BAR to be the input to sed, with the output being assigned back to
BAR? If so, that's not what this is doing. In fact, AFAICT you can't do
BAR=| echo $BAR |sed ... either. Another variable is required as I did
above. The example you gave will pipe the entire message to sed, and
assign the output to BAR. Maybe that's what you were seeing and it
wasn't what you expected. Other than that, I have no ideas.
Don Hammond \ / ASCII Ribbon Campaign
Raleigh, NC US X Against HTML Mail,
/ \ and News Too
procmail mailing list