Sujet : Re: Using << and an output pipe together in shell (bash)
De : nn.throttle (at) *nospam* xoxy.net (Helmut Waitzmann)
Groupes : comp.unix.shellDate : 03. Nov 2024, 21:37:07
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <83ikt4vyto.fsf@helmutwaitzmann.news.arcor.de>
References : 1
User-Agent : Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
gazelle@shell.xmission.com (Kenny McCormack):
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.
>
I don't think so. It doesn't see any command after the pipe
symbol (because the next and following lines are the here
document). That's why GVIM shows the color used to indicate
strings and here documents.
In the end, I ended up replacing the " << EOF | ANotherCommand
..." construct with:
>
>
> >(ANotherCommand ...) << EOF
>
and all is well. But why? Should this be necessary?
>
It isn't necessary, because there are at least three
alternatives.
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: > >(...))
>
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"