Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Here Doc Trouble

25 views
Skip to first unread message

Tom

unread,
May 10, 2012, 7:53:42 AM5/10/12
to
Greetings,

I have the following bash code:

#!/bin/bash
x=0
/bin/cat >bar <<End-of-message
This is a test message.
End-of-message

if [ $x == 0 ]; then
echo "This Shouldn't be printed in the HERE Doc..."
fi

Unfortunately when I run this everything gets printed to the "bar"
file. I've tried the "-" for the "<<-", quotes, changing to just EOF
or END, nothing has helped. This on a RH machine running 5.5. and the
bash shell is:

% bash --version
GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.


Any ideas what I might be doing wrong here? Thanks for any help in
advance!

Tom

Marc Girod

unread,
May 12, 2012, 10:00:07 AM5/12/12
to
On May 10, 12:53 pm, Tom <zi...@yahoo.com> wrote:

> I've tried the "-" for the "<<-", quotes, changing to just EOF
> or END, nothing has helped. This on a RH machine running 5.5. and the
> bash shell is:

It only ever used small words for the here strings: eot, eot1, or
'eot'...
It never came to my mind to use dashes or anything long...

Anyway, the only thing I can think of is that you have a space after
'End-of-message'...
Tried on cygwin with bash 4.1.10(4).

Marc

Chris F.A. Johnson

unread,
May 14, 2012, 6:39:12 PM5/14/12
to
On 2012-05-10, Tom wrote:
> Greetings,
>
> I have the following bash code:
>
> #!/bin/bash
> x=0
> /bin/cat >bar <<End-of-message
> This is a test message.
> End-of-message
>
> if [ $x == 0 ]; then
> echo "This Shouldn't be printed in the HERE Doc..."
> fi
>
> Unfortunately when I run this everything gets printed to the "bar"
> file. I've tried the "-" for the "<<-", quotes, changing to just EOF
> or END, nothing has helped. This on a RH machine running 5.5. and the
> bash shell is:
>
>% bash --version
> GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
> Copyright (C) 2005 Free Software Foundation, Inc.

It works as expected in all versions I've tried, from 2.05b to 4.2


--
Chris F.A. Johnson, author <http://shell.cfajohnson.com/>
===================================================================
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)

Barry Margolin

unread,
May 14, 2012, 9:11:29 PM5/14/12
to
In article
<635b7fc4-4140-425b...@y14g2000yqd.googlegroups.com>,
Is it possible that in your actual script you have the End-of-message
line indented?

If you use the <<- form, you can indent the End-of-message line with
tabs, but NOT spaces.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Ed Morton

unread,
May 15, 2012, 11:11:12 AM5/15/12
to
Tom <zi...@yahoo.com> wrote:

> Greetings,
>
> I have the following bash code:
>
> #!/bin/bash
> x=0
> /bin/cat >bar <<End-of-message
> This is a test message.
> End-of-message
>
> if [ $x == 0 ]; then

The shell test for equality is "=" not "==".

> echo "This Shouldn't be printed in the HERE Doc..."
> fi
>
> Unfortunately when I run this everything gets printed to the "bar"
> file. I've tried the "-" for the "<<-", quotes, changing to just EOF
> or END, nothing has helped. This on a RH machine running 5.5. and the
> bash shell is:
>
> % bash --version
> GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
> Copyright (C) 2005 Free Software Foundation, Inc.
>
>
> Any ideas what I might be doing wrong here? Thanks for any help in
> advance!
>
> Tom
>

Show us exactly what is happening. Assuming your script is in "foo", do this:

$ > bar
$ cat -v foo
$ ./foo
$ cat -v bar

and copy/paste those commands plus their output into a NG response.

Ed.


Posted using www.webuse.net

Chris F.A. Johnson

unread,
May 15, 2012, 2:19:35 PM5/15/12
to
On 2012-05-15, Ed Morton wrote:
> Tom <zi...@yahoo.com> wrote:
>
>> Greetings,
>>
>> I have the following bash code:
>>
>> #!/bin/bash
>> x=0
>> /bin/cat >bar <<End-of-message
>> This is a test message.
>> End-of-message
>>
>> if [ $x == 0 ]; then
>
> The shell test for equality is "=" not "==".

But bash accepts both.

Thomas 'PointedEars' Lahn

unread,
May 16, 2012, 8:50:04 AM5/16/12
to
Ed Morton wrote:

> Tom <zi...@yahoo.com> wrote:
>> I have the following bash code:
>>
>> #!/bin/bash
>> x=0
>> /bin/cat >bar <<End-of-message
>> This is a test message.
>> End-of-message
>>
>> if [ $x == 0 ]; then
>
> The shell test for equality is "=" not "==".

That is not entirely correct. The OP is using a certain shell, bash, where
`==' is an equality operator with special features:

| When the == and != operators are used, the string to the right of
| the operator is considered a pattern and matched according to the
| rules described below under Pattern Matching. If the shell option
| nocase‐match is enabled, the match is performed without regard to
| the case of alphabetic characters. The return value is 0 if the
| string matches (==) or does not match (!=) the pattern, and 1
| otherwise. Any part of the pattern may be quoted to force it to
| be matched as a string.
|
| […]
| ARITHMETIC EVALUATION
| […]
| == != equality and inequality
| […]
| GNU Bash-4.2 2010 December 28 BASH(1)

>> […]
>> % bash --version
>> GNU bash, version 3.2.25(1)-release (x86_64-redhat-linux-gnu)
>> Copyright (C) 2005 Free Software Foundation, Inc.

That it is not working may have to do with the old bash version.

--
PointedEars

Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Jerry Peters

unread,
May 16, 2012, 5:04:16 PM5/16/12
to
Thomas 'PointedEars' Lahn <Point...@web.de> wrote:
> Ed Morton wrote:
>
>> Tom <zi...@yahoo.com> wrote:
>>> I have the following bash code:
>>>
>>> #!/bin/bash
>>> x=0
>>> /bin/cat >bar <<End-of-message
>>> This is a test message.
>>> End-of-message
>>>
>>> if [ $x == 0 ]; then
>>
>> The shell test for equality is "=" not "==".
>
> That is not entirely correct. The OP is using a certain shell, bash, where
> `==' is an equality operator with special features:
>
> | When the == and != operators are used, the string to the right of
> | the operator is considered a pattern and matched according to the
> | rules described below under Pattern Matching. If the shell option
> | nocase?match is enabled, the match is performed without regard to
> | the case of alphabetic characters. The return value is 0 if the
> | string matches (==) or does not match (!=) the pattern, and 1
> | otherwise. Any part of the pattern may be quoted to force it to
> | be matched as a string.

You omitted the lead-in to that paragraph:

[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the
conditional expression expression. Expressions are composed of
the primaries described below under CONDITIONAL EXPRESSIONS.
Word splitting and pathname expansion are not performed on the
words between the [[ and ]]; tilde expansion, parameter and
variable expansion, arithmetic expansion, command substitution,
process substitution, and quote removal are performed. Condi-
tional operators such as -f must be unquoted to be recognized as
primaries.

When used with [[, The < and > operators sort lexicographically
using the current locale.

When the == and != operators are used, the string to the right
of the operator is considered a pattern and matched according to
the rules described below under Pattern Matching.

Note that the OP only used single '[ ]' not '[[ ]]'.

Jerry

> |
> | [?]
> | ARITHMETIC EVALUATION
> | [?]
> | == != equality and inequality
> | [?]
> | GNU Bash-4.2 2010 December 28 BASH(1)
>
>>> [?]

Thomas 'PointedEars' Lahn

unread,
Jun 1, 2012, 10:33:46 AM6/1/12
to
Jerry Peters wrote:

> Thomas 'PointedEars' Lahn <Point...@web.de> wrote:
>> Ed Morton wrote:
>>> Tom <zi...@yahoo.com> wrote:
>>>> I have the following bash code:
>>>>
>>>> #!/bin/bash
>>>> x=0
>>>> /bin/cat >bar <<End-of-message
>>>> This is a test message.
>>>> End-of-message
>>>>
>>>> if [ $x == 0 ]; then
>>>
>>> The shell test for equality is "=" not "==".
>>
>> That is not entirely correct. The OP is using a certain shell, bash,
>> where `==' is an equality operator with special features:
>>
>> | When the == and != operators are used, the string to the right
>> | of the operator is considered a pattern and matched according to
>> | the rules described below under Pattern Matching. If the shell
>> | option nocase?match is enabled, the match is performed without
>> | regard to
>> | the case of alphabetic characters. The return value is 0 if the
>> | string matches (==) or does not match (!=) the pattern, and 1
>> | otherwise. Any part of the pattern may be quoted to force it to
>> | be matched as a string.
>
> You omitted the lead-in to that paragraph:

You omitted your e-mail address.

> [[ expression ]]

That is _not_ the lead-in to the paragraph above; it is:

| Compound Commands

However:

$ bash -vxc '[ 1 == 0 ] && echo 42'
[ 1 == 0 ] && echo 42
+ '[' 1 == 0 ']'

$ bash -vxc '[ 1 == 1 ] && echo 42'
[ 1 == 1 ] && echo 42
+ '[' 1 == 1 ']'
+ echo 42
42

$ bash --version
GNU bash, version 4.2.20(1)-release (i486-pc-linux-gnu)
[…]

The reason is:

| CONDITIONAL EXPRESSIONS
| Conditional expressions are used by the [[ compound command and
| the test and [ builtin commands to test file attributes and
| perform string and arithmetic comparisons. Expressions are formed
| from the following unary or binary primaries. […]
|
| […]
| string1 == string2
| string1 = string2
| True if the strings are equal. = should be used with the
| test command for POSIX conformance.
| […]
| SHELL BUILTIN COMMANDS
| […]
|
| test expr
| [ expr ]
|
| Return a status of 0 or 1 depending on the evaluation of the
| conditional expression expr. Each operator and operand must
| be a separate argument. Expressions are composed of the
| primaries described above under CONDITIONAL EXPRESSIONS. […]

> When used with [[, The < and > operators sort
> lexicographically using the current locale.
>
> When the == and != operators are used, the string to the
> right of the operator is considered a pattern and matched
> according to the rules described below under Pattern
> Matching.
>
> Note that the OP only used single '[ ]' not '[[ ]]'.

Falsifying quotations meets a supposed anti-social character.
0 new messages