Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading with cat: Stop when not receiving data

Is there any way to tell the cat command to stop reading when not receiving any data? maybe with some "timeout" that specifies for how long no data is incoming.

Any ideas?

like image 367
Kai Avatar asked Sep 01 '11 12:09

Kai


People also ask

How do I get out of cat mode?

We can see that whatever texts we've entered into the standard input stream will be echoed to the output stream by the cat command. Once we are done, we can terminate the command by pressing CTRL+D.

How do you stop the cat command?

We have already seen one use of the cat command to write the contents of a file to the screen. Then type a few words on the keyboard and press the [Return] key. Finally hold the [Ctrl] key down and press [d] (written as ^D for short) to end the input.

What does cat << EOF mean?

This operator stands for the end of the file. This means that wherever a compiler or an interpreter encounters this operator, it will receive an indication that the file it was reading has ended.

How do I interrupt a cat in Linux?

How to exit or stop cat command? In order to stop/exit/quit cat command, you can simply press key q, you should return to the prompt.


1 Answers

There is a timeout(1) command. Example:

timeout 5s cat /dev/random

Dependening on your circumstances. E.g. you run bash with -e and care normally for the exit code.

timeout 5s cat /dev/random || true
like image 102
Michał Šrajer Avatar answered Oct 30 '22 06:10

Michał Šrajer