Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 43 |
Nodes: | 6 (0 / 6) |
Uptime: | 108:23:43 |
Calls: | 290 |
Files: | 905 |
Messages: | 76,683 |
Consider this code in a bash script:
(Note: No actual indentation is intended - only shown like this
for posting)
someCommand ... << EOF | someOtherCommand
some data
for someCommand
EOF
This should work, right?
In simple cases, it does (seem to) work OK.
However, in the actual real world case, it is more like:
someCommand -x -y "sjdfhk" and more options \
-g and still more options -Q "another option" << EOF |
/usr/lib/ANotherCommand -x "with option" and "more options"
some data
for someCommand
EOF
This time, two things happen:
When edited with GVIM, everything after the line that ends with
| is highlighted as if it was an unterminated string (that is,
in a purple/pink color) and when the above file is dotted, bash
complains about "Syntax error: Unexpected end of file" - as if
it never seems the EOF tag.
In the end, I ended up replacing the " << EOF | ANotherCommand
..." construct with:
> >(ANotherCommand ...) << EOF
and all is well. But why? Should this be necessary?
Yes, I know it is hard to debug this w/o specifics, but I really
don't think the specifics matter; they would be hard to
reproduce here.
I just want to know if the basic syntax is valid and/or if there is a
better workaround (better than switching to: > >(...))
gazelle@shell.xmission.com (Kenny McCormack):
I just want to know if the basic syntax is valid and/or if there is a
better workaround (better than switching to: > >(...))
Either use a backslash for unfolding a folded line (as Ben
showed) or put the part of the command line following the pipe
symbol ("|") after the EOF of the here document (as Janis showed)
or embrace the simple command and its here document by "{ ... ;
}":
{
someCommand -x -y "sjdfhk" and more options \
-g and still more options -Q "another option" << EOF
some data
for someCommand
EOF
} |
/usr/lib/ANotherCommand -x "with option" and "more options"
Consider this code in a bash script:
(Note: No actual indentation is intended - only shown like this for posting)
someCommand ... << EOF | someOtherCommand
some data
for someCommand
EOF
This should work, right?
In simple cases, it does (seem to) work OK.
However, in the actual real world case, it is more like:
someCommand -x -y "sjdfhk" and more options \
-g and still more options -Q "another option" << EOF |
/usr/lib/ANotherCommand -x "with option" and "more options"
some data
for someCommand
EOF
This time, two things happen:
When edited with GVIM, everything after the line that ends with | is highlighted as if it was an unterminated string (that is, in a purple/pink color) and when the above file is dotted, bash complains about "Syntax
error: Unexpected end of file" - as if it never seems the EOF tag.
In the end, I ended up replacing the " << EOF | ANotherCommand ..." construct with:
> >(ANotherCommand ...) << EOF
and all is well. But why? Should this be necessary?
Yes, I know it is hard to debug this w/o specifics, but I really don't
think the specifics matter; they would be hard to reproduce here.
I just want to know if the basic syntax is valid and/or if there is a
better workaround (better than switching to: > >(...))
I think you need "| \" at the end of this line. At least that's what I >usually do and it seems to work.
Consider this code in a bash script:
(Note: No actual indentation is intended - only shown like this for posting)
someCommand ... << EOF | someOtherCommand
some data
for someCommand
EOF
This should work, right?
In simple cases, it does (seem to) work OK.
However, in the actual real world case, it is more like:
someCommand -x -y "sjdfhk" and more options \
-g and still more options -Q "another option" << EOF |
/usr/lib/ANotherCommand -x "with option" and "more options"
some data
for someCommand
EOF
In article <87r07v99wd.fsf@bsb.me.uk>, Ben Bacarisse <ben@bsb.me.uk> wrote: ...
I think you need "| \" at the end of this line. At least that's what I
usually do and it seems to work.
It is not generally necessary to put a backslash at the end of a line that ends with | (in shell script).
But it *is* necessary in this special case!
So, you get the prize.
Note that this solves it as far as getting bash to be happy with it is concerned. When I get a chance, I need to see about how VIM feels about it.
Here's test case:
$ nl << EOF | nl
test
this
EOF
1 1 test
2 2 this
$ nl << EOF |
nl
test
this
EOF
-bash5: syntax error: unexpected end of file
Status: 2
$ nl << EOF | \
nl
test
this
EOF
1 1 test
2 2 this
$
Unless you put the 'nl' at the "right place"; you can write your
example below as
$ nl << EOF |
test
this
EOF
nl
so the backslash is not "necessary". - As you say, the '|' needs no >[spurious] continuation escape character if you have it at the end
of a command. - After the lines that define the here-doc (for 'nl's >redirection) the pipe command gets continued on the subsequent line,
which is the line after the "EOF".
In article <87r07v99wd.fsf@bsb.me.uk>, Ben Bacarisse <ben@bsb.me.uk> wrote: ...
I think you need "| \" at the end of this line. At least that's what I >>usually do and it seems to work.
It is not generally necessary to put a backslash at the end of a line that ends with | (in shell script).
foofoo
EOF
bar
EOF
xyzzy
EOF
I think I actually prefer to stick with; > >(cmd ...)
startstart
EOF