I am running some scripts with commands having cat
pipelined with grep
like:
cat file.txt | grep "pattern"
Most of the times there are no problems. But sometimes I get below error:
cat: write error: Broken pipe
So how do I find out when the command is causing this problem and why?
This error generally means means that the data stopped flowing to us and we were unable to start the transfer again. Often times this is caused by a wireless internet connection with fluctuating signal strength, a firewall or other security software.
A broken Pipe Error is generally an Input/Output Error, which is occurred at the Linux System level. The error has occurred during the reading and writing of the files and it mainly occurs during the operations of the files.
The reason is because the pipe is closed by grep
when it still has some data to be read from cat
. The signal SIGPIPE
is caught by cat and it exits.
What usually happens in a pipeline is the shell runs cat
in one process and grep
in another. The stdout of cat
is connected to the write-end of the pipe and stdin of grep
to the read end. What happened was grep
hit a pattern search that did not exist and exited immediately causing the read end of the pipe to be closed, which cat
does not like since it has some more data to be write out to the pipe. Since the write actions happens to an other which has been closed other end, SIGPIPE
is caught by the cat
on which it immediately exits.
For such a trivial case, you could remove the pipeline usage altogether and run it as grep "pattern" file.txt
when the file's contents are made available over the stdin of grep
on which it could read from.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With