Discussion:
Newlines in mime-construct
Todd A. Jacobs
2008-09-29 19:22:43 UTC
Permalink
I'm having a problem with creating newlines using the perl program
mime-construct from the bash shell. For example:

mime-construct --output --to nospam-S/***@public.gmane.org \
--subject Test --encoding 7bit --string "Foo\nbar" \
--file-attach test.doc | sendmail

puts a literal "\n" (not a newline) into the message. Is this a bug, or
am I failing to escape things properly somehow?
--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"
Charles Galpin
2008-09-29 20:11:44 UTC
Permalink
Post by Todd A. Jacobs
I'm having a problem with creating newlines using the perl program
--subject Test --encoding 7bit --string "Foo\nbar" \
--file-attach test.doc | sendmail
puts a literal "\n" (not a newline) into the message. Is this a bug, or
am I failing to escape things properly somehow?
Hi Todd

Isn't this just a shell globbing issue?

-bash-2.05b$ echo "Foo\nBar"
Foo\nBar
-bash-2.05b$

The string you are passing to the program is exactly that.

charles
Todd A. Jacobs
2008-09-29 21:19:57 UTC
Permalink
Post by Charles Galpin
Test --encoding 7bit --string "Foo\nbar" \ --file-attach test.doc
| sendmail
puts a literal "\n" (not a newline) into the message. Is this a bug,
or am I failing to escape things properly somehow?
Isn't this just a shell globbing issue?
Apparently so, but I hadn't thought of it that way because I thought
that the shell would expand "\n" or "^M" inside quotes, which apparently
it doesn't. The bash manual says:

Words of the form $'string' are treated specially. The word expands
to string, with backslash-escaped characters replaced as specified
by the ANSI C standard.

so I would expect "Foo$'\n'Bar" to expand properly, but it doesn't.
Command substitution seems to work, though:

--string "$(echo -e "Foo\nbar")"

I'm not sure why the shell isn't properly handling the escape directly,
though. Why isn't the expansion happening as I expect?
--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"
Charles Galpin
2008-09-29 22:01:44 UTC
Permalink
Post by Todd A. Jacobs
I'm not sure why the shell isn't properly handling the escape
directly,
though. Why isn't the expansion happening as I expect?
Because you are expecting the wrong thing? Hehe, seriously the snippet
of man page you quote is about variables, not quoted strings. So my
guess is that quoted strings are treated as string literals, and all
your double quotes are doing are preventing word splitting of the
argument value. See the EXPANSION section but as I read it, there is
nothing to expand and the quotes get stripped.

charles
Cameron Simpson
2008-09-30 00:25:39 UTC
Permalink
On 29Sep2008 14:19, Todd A. Jacobs <nospam-S/***@public.gmane.org> wrote:
| On Mon, Sep 29, 2008 at 04:11:44PM -0400, Charles Galpin wrote:
|
| > > Test --encoding 7bit --string "Foo\nbar" \ --file-attach test.doc
| > > | sendmail
| > >
| > > puts a literal "\n" (not a newline) into the message. Is this a bug,
| > > or am I failing to escape things properly somehow?
|
| > Isn't this just a shell globbing issue?
|
| Apparently so,

There's no globbing involved here. Quoting, maybe.

| but I hadn't thought of it that way because I thought
| that the shell would expand "\n" or "^M" inside quotes, which apparently
| it doesn't.

Never has. The echo command does on some platforms.

| The bash manual says:
| Words of the form $'string' are treated specially. The word expands
| to string, with backslash-escaped characters replaced as specified
| by the ANSI C standard.
| so I would expect "Foo$'\n'Bar" to expand properly, but it doesn't.

It expands properly. But not in the incantation you hav supplied.

Why have you not tried

Foo$'\n'Bar

or

$'Foo\nBar'

instead of your more complex (and unsupported by the doco) attempt?
"Words" is a technical term; inside a string you're _inside_ a word.
This isn't plain old parameter substitution.

| Command substitution seems to work, though:
|
| --string "$(echo -e "Foo\nbar")"

Again, overly complex. Try:

--string $'Foo\nbar'

| I'm not sure why the shell isn't properly handling the escape directly,
| though. Why isn't the expansion happening as I expect?

Because you misunderstand the documentation. Look up "word" carefully.
It's a particular piece of shell syntax. Because "word"s are an end
product of space separation, you need not worry about the newline
breaking up the string - the breakup has already happened.

Cheers,
--
Cameron Simpson <cs-***@public.gmane.org> DoD#743
http://www.cskk.ezoshosting.com/cs/
Todd A. Jacobs
2008-09-30 01:20:12 UTC
Permalink
Post by Cameron Simpson
Why have you not tried
Foo$'\n'Bar
Because it's a contrived example. If you're quoting a real sentence,
you'd have space issues:

"This is a \n new line."

Unless you escaped each space individually, or excluded the newline. For
whatever it's worth, this works:

"This is a "$'\n'"new line."

but is extremely ugly. :) This is marginally less ugly, and will work as
long as there are no apostrophes or single-quotes in the string:

$'This is a \nnew line.'

So, the mystery is solved. Thanks for everyone's help.
--
"Oh, look: rocks!"
-- Doctor Who, "Destiny of the Daleks"
Loading...