Unix Programming - piping a group of "echo" to a program

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > May 2007 > piping a group of "echo" to a program





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author piping a group of "echo" to a program
rhXX

2007-05-30, 7:17 am

hi all,

i want to send a group of lines to a program in a script shell.

now i'm using:

echo "line 1" > $TMPFILE
echo "line 2" >> $TMPFILE
....
echo "line N" >> $TMPFILE

cat $TMPFILE | prog

rm -f $TMPFILE

it seems me very primitive, create / delete a temp file only to
"group" the lines for piping

is it any method to pipe these line directly without temp file?

tks in advance

Joachim Schmitz

2007-05-30, 7:17 am

"rhXX" <rh00667@gmail.com> schrieb im Newsbeitrag
news:1180525019.510977.253920@w5g2000hsg.googlegroups.com...
> hi all,
>
> i want to send a group of lines to a program in a script shell.
>
> now i'm using:
>
> echo "line 1" > $TMPFILE
> echo "line 2" >> $TMPFILE
> ...
> echo "line N" >> $TMPFILE
>
> cat $TMPFILE | prog
>
> rm -f $TMPFILE
>
> it seems me very primitive, create / delete a temp file only to
> "group" the lines for piping
>
> is it any method to pipe these line directly without temp file?

'here-document' is what you need.

cat | prog <<-EOF
line 1
line 2
....
line N
EOF

Bye, Jojo


Joachim Schmitz

2007-05-30, 7:17 am


"Joachim Schmitz" <nospam.schmitz@hp.com> schrieb im Newsbeitrag
news:f3jo15$m9b$1@usenet01.boi.hp.com...
> "rhXX" <rh00667@gmail.com> schrieb im Newsbeitrag
> news:1180525019.510977.253920@w5g2000hsg.googlegroups.com...
> 'here-document' is what you need.
>
> cat | prog <<-EOF
> line 1
> line 2
> ...
> line N
> EOF

Uhm... useless use of cat award... better and shorter:

prog <<-EOF
line 1
line 2
...
line N
EOF

Bye, Jojo


rhXX

2007-05-30, 1:25 pm

ok, tks to all for inmediatly help!

sincerely

Logan Shaw

2007-05-31, 1:22 am

rhXX wrote:
> hi all,
>
> i want to send a group of lines to a program in a script shell.
>
> now i'm using:
>
> echo "line 1" > $TMPFILE
> echo "line 2" >> $TMPFILE
> ...
> echo "line N" >> $TMPFILE
>
> cat $TMPFILE | prog
>
> rm -f $TMPFILE
>
> it seems me very primitive, create / delete a temp file only to
> "group" the lines for piping
>
> is it any method to pipe these line directly without temp file?


One person already gave an example that uses the "<<EOF" syntax,
but if you really need to group commands' output together into
a pipe, and you have to commands (and not just interpolated strings),
you can do this:

{
echo "line 1"
echo "line 2"
echo "line 3"
} | prog

Personally I like this since it is more flexible. You can chain
them:

{ echo 1; echo 2; } | { read x; read y; echo "$x / $y"; } | bc -l

Or nest them:

{
echo 1
{
echo 2
echo 3
} | sed -e 's/$/x/'
echo 4
} | sed -e 's/$/y/'

Also, in some cases, a better solution might be to use a function.

- Logan
rhXX

2007-05-31, 7:21 am

logan,

nice, tks a lot!

On May 31, 3:02 am, Logan Shaw <lshaw-use...@austin.rr.com> wrote:
> rhXX wrote:
>
>
>
>
>
>
>
>
> One person already gave an example that uses the "<<EOF" syntax,
> but if you really need to group commands' output together into
> a pipe, and you have to commands (and not just interpolated strings),
> you can do this:
>
> {
> echo "line 1"
> echo "line 2"
> echo "line 3"
> } | prog
>
> Personally I like this since it is more flexible. You can chain
> them:
>
> { echo 1; echo 2; } | { read x; read y; echo "$x / $y"; } | bc -l
>
> Or nest them:
>
> {
> echo 1
> {
> echo 2
> echo 3
> } | sed -e 's/$/x/'
> echo 4
> } | sed -e 's/$/y/'
>
> Also, in some cases, a better solution might be to use a function.
>
> - Logan



Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com