Posted: Fri Jul 04, 2008 8:10 am Post subject: How to accept input from stdin?
Hi,
I try to make a wrapper around an existing program, which would behave
exactly the same as the original one. But my following attempt was
failed. Would you pleaes let me know what is the correct way?
Thanks,
Peng
$cat ./my_tee.sh
#!/bin/bash
read
echo $REPLY | tee $*
$ cat abc
a
b
c
$ cat abc | ./my_tee.sh efg
a
I try to make a wrapper around an existing program, which would behave
exactly the same as the original one. But my following attempt was
failed. Would you pleaes let me know what is the correct way?
Thanks,
Peng
$cat ./my_tee.sh
#!/bin/bash
read
echo $REPLY | tee $*
$ cat abc
a
b
c
$ cat abc | ./my_tee.sh efg
a
What about
cat abc | efg
or, better
efg < abc
or, better yet, explain what you're trying to do?
--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
I try to make a wrapper around an existing program, which would behave
exactly the same as the original one. But my following attempt was
failed. Would you pleaes let me know what is the correct way?
Thanks,
Peng
$cat ./my_tee.sh
#!/bin/bash
read
echo $REPLY | tee $*
$ cat abc
a
b
c
$ cat abc | ./my_tee.sh efg
a
IFS=
while read; do
echo "$REPLY"
done |tee "$@"
or
IFS=
while read; do
echo "$REPLY" | tee -a "$@"
done
I try to make a wrapper around an existing program, which would behave
exactly the same as the original one. But my following attempt was
failed. Would you pleaes let me know what is the correct way?
That depends on what you want to do.
Quote:
$cat ./my_tee.sh
#!/bin/bash
read
echo $REPLY | tee $*
$ cat abc
a
b
c
$ cat abc | ./my_tee.sh efg
a
How about:
tee "$@" | do_whatever
Replace 'do_whatever' with whatever you want to do to the input.
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Posted: Fri Jul 04, 2008 8:10 am Post subject: Re: How to accept input from stdin?
On Jul 3, 2:24 pm, "PengYu...@gmail.com" <PengYu...@gmail.com> wrote:
Quote:
Hi,
I try to make a wrapper around an existing program, which would behave
exactly the same as the original one. But my following attempt was
failed. Would you pleaes let me know what is the correct way?
Thanks,
Peng
$cat ./my_tee.sh
#!/bin/bash
read
echo $REPLY | tee $*
$ cat abc
a
b
c
$ cat abc | ./my_tee.sh efg
a
most shells support "here-docs" so something like:
I try to make a wrapper around an existing program, which would behave
exactly the same as the original one. But my following attempt was
failed. Would you pleaes let me know what is the correct way?
Thanks,
Peng
$cat ./my_tee.sh
#!/bin/bash
read
echo $REPLY | tee $*
$ cat abc
a
b
c
$ cat abc | ./my_tee.sh efg
a
What about
cat abc | efg
or, better
efg < abc
or, better yet, explain what you're trying to do?
I'm trying to mimic an existing command. Of course, in practice, the
new command is different from the old one, otherwise, this is no
point. As a simplification, in my example, the new command (my_tee.sh)
shall do the same thing as the old command (tee).
The point is that I want the new one accept exactly the same set of
command line arguments as well as the stdin as the old one.
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum