[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] How not to wait for the inputs finish if a program does
From: |
John McKown |
Subject: |
Re: [Help-bash] How not to wait for the inputs finish if a program does not need to see all the input? |
Date: |
Fri, 25 Mar 2016 21:06:51 -0500 |
On Fri, Mar 25, 2016 at 4:40 PM, Ehsan Hajiramezanali <
address@hidden> wrote:
> Hi,
>
> If one runs the following examples, both head and awk will wait until
> `sleep` is finished.
>
> (echo 1; sleep 10; seq 10) | head -n 1
> (echo 1; sleep 10; seq 10) | awk -e 'NR==1{print $1;exit}'
>
> However, they only need the output from the first echo command. It is
> unnecessary that they wait until `sleep` is finished. I am wondering
> if this problem is related with the shell or the programs head/awk.
>
> How one might let head and awk finish as soon as they see the first line?
>
That was an interesting question. In the case of head, it is the head
program. I did the following sequence:
echo -e 'a\n\b\nc' | strace -o trace -ff head -n 1
It turns out that head does not do something like gets(), but does a read()
to read a "block" of data. In particular, I saw:
read(0, "a\nb\nc\n\n", 8192) = 7
So it is due to the way that head is written. Looking at awk similarly:
echo -e 'a\n\b\nc' | strace -o trace -ff awk -e 'NR==1 {print $1;exit}'
it also showed the same thing: awk does not actually read "lines", but
"blocks". The trace showed the entry:
read(0, "a\nb\nc\n\n", 4096) = 7
But, as interesting as this is, I am of the _opinion_ that the "problem" is
that the BASH shell upon which you did this does not display a prompt until
the subshell exits. And the subshell is what is sleeping. I did the
following from one xterm: (echo -e "a\n\b\nc";sleep 10m;echo "f") | head -n
1
on another xterm, I did a "ps -Af | fgrep myid | less -S". The head
command was nowhere to be found in the list of processes, so it must have
ended _and_ BASH must have reaped it otherwise it would have been [defunct]
in the list.
So, this appears to be a shell related effect.
> Thanks in advance.
>
> Best regards,
> Ehsan
>
>
--
How many surrealists does it take to screw in a lightbulb? One to hold the
giraffe and one to fill the bathtub with brightly colored power tools.
Maranatha! <><
John McKown