help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Interactive vs non-interactive behavior


From: DJ Mills
Subject: Re: [Help-bash] Interactive vs non-interactive behavior
Date: Mon, 11 Sep 2017 15:20:25 -0400

http://mywiki.wooledge.org/BashFAQ/009

The output is getting buffered in the pipe

On Mon, Sep 11, 2017 at 11:06 AM, Kazama Seiji <address@hidden> wrote:

> Hey guys.
>
>
> I've got such code sample:
>
> timeout 5 tail -fn0 access.log | awk '{print $1}'
>
>
> It listens to nginx's access.log for 5 secs and outputs the log's first
> column (=client IP).
>
>
> The sample works the same in interactive shell as well as a standalone
> script.
>
>
> But as only I add more stages to the pipe it fails in interactive shell
> like in:
>
> timeout 5 tail -fn0 access.log | awk '{print $1}' | wc -l
>
> or
>
> timeout 5 tail -fn0 access.log | awk '{print $1}' | sort | uniq -c | sort
> -k1,1nr -k2,2V
>
>
> the output is a single line "Terminated". It still works as a standalone
> script though.
>
>
> I've figured out the enabled monitor=on bash setting for interactive mode
> triggers the behavior. So with "set +m" applied it works in interactive
> shell. Another way to overcome it in interactive shell is such trick
>
> ( timeout 5 tail -fn0 access.log; true ) | awk '{print $1}' | wc -l
>
>
> The question is: what is so special to pipes of 3+ stages that it presents
> different behavior vs a 2-stage pipe when monitor=on in bash?
> E.g. why this works
> set -m
> timeout 5 tail -fn0 access.log | awk '{print $1}'
>
> but this doesn't
> set -m
> timeout 5 tail -fn0 access.log | awk '{print $1}' | wc -l
>
> (I explicitly set monitor=on above for clarity.)
>
>


reply via email to

[Prev in Thread] Current Thread [Next in Thread]