On Wed, Nov 11, 2009 at 6:22 AM, Jeff Chua <jeff.chua.linux@gmail.com>wrote:
> On Wed, Nov 11, 2009 at 12:04 PM, Jeff Chua <jeff.chua.linux@gmail.com
> >wrote:
>
> >
> >
> > On Wed, Nov 11, 2009 at 12:44 AM, Chet Ramey <chet.ramey@case.edu>
> wrote:
> >
> >> > How do you silent this one without a subshell.
> >>
> >> What's wrong with the approach above?
> >>
> >
> > Nothing wrong, but can be made more efficient because "| grep" means
> > another subprocess which can be eliminated if the shell silents the
> > Terminate command in the first place.
> >
> > #!/bin/bash
> > {
> > sleep 60 &
> > P=$!
> > kill $P
> > sleep 1
> > } 2>&1 | grep -v " Terminated"
> > exit
> >
>
>
> Extending the example above slighting ... now with grep means I can't see
> the message real-time anymore ... if you try the example below without the
> grep, it should display 0 1 2 ... every second.
>
> #!/bin/bash
> {
> for((i = 0; i <100; i++))
> do
> echo " $i\c"
> sleep 1
> done &
> P=$!
> sleep 10
> kill $P
> sleep 1
> echo ok
> } 2>&1 | grep -v " Terminated"
> exit
>
I guess Chet meant his approach, not yours:
#!/bin/bash
for((i = 0; i <100; i++))
do
echo " $i\c"
sleep 1
done &
P=$!
sleep 10
exec 3>&1 2>/dev/null #save stderr on fd3, redirect stderr to /dev/null
kill $P
wait $P
exec 2>&3- # restore stderr, close fd3
echo ok