Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash tail command and multiple pipes

Tags:

bash

pipe

I am currently facing a problem with the command

while sleep 0.3; do echo "1"; done | tail -n +3 | grep --line-buffered "1"

I wanted an output that looks like:

[nothing during 0.6s]
1
1
...

but had:

[nothing forever]

I found this question and read this blog post but as the --line-buffered option is set for grep, I think this is not a buffering problem. My researches did not led me to other solutions, I hope I did not miss something obvious.

I am using zsh 5.7.1 (x86_64-pc-linux-gnu) if it is important.

like image 498
Adrien Suau Avatar asked Jun 14 '19 22:06

Adrien Suau


People also ask

Can you have more than one pipe in Linux?

One of the most powerful shell operators is the pipe ( | ). The pipe takes output from one command and uses it as input for another. And, you're not limited to a single piped command—you can stack them as many times as you like, or until you run out of output or file descriptors.

How can you use pipe in multiple commands?

You can make it do so by using the pipe character '|'. Pipe is used to combine two or more commands, and in this, the output of one command acts as input to another command, and this command's output may act as input to the next command and so on.

What is the tail command in bash?

The tail command, as the name implies, print the last N number of data of the given input. By default it prints the last 10 lines of the specified files. If more than one file name is provided then data from each file is precedes by its file name.

What will tail 10 command do?

Tail is a command which prints the last few number of lines (10 lines by default) of a certain file, then terminates. Example 1: By default “tail” prints the last 10 lines of a file, then exits. as you can see, this prints the last 10 lines of /var/log/messages.


1 Answers

As Barmar mentioned in the comments, this is getting swallowed up in a 4KB stdio buffer. You can use unbuffer -p to avoid it:

while sleep 0.3; do echo "1"; done | unbuffer -p tail -n +3 | grep 1

Or you could also use stdbuf in line-buffering mode:

while sleep 0.3; do echo "1"; done | stdbuf -oL tail -n +3 | grep 1
like image 63
Jeff Bowman Avatar answered Sep 28 '22 17:09

Jeff Bowman