cat command with timeout
Nadav Har'El
nyh at math.technion.ac.il
Thu Jun 10 10:48:38 IDT 2010
On Thu, Jun 10, 2010, Ori Idan wrote about "cat command with timeout":
> I have to read lines from serial port, I thought to read them using 'cat'
> however, since the other unit connected to the serial port, might
> malfunction and not send anything, I need a timeout to stop reading after
> few seconds.
> Does someone know of an alternative to cat with timeout?
It sounds like (but correct me if I'm wrong) that what you're looking for
is't "cat", but rather a way to read a single line in a shell-script, but
give up after a timeout when you didn't get any line. The command for this
is "read", and bash has a -t parameter:
read -t 4 line
reads a single line from the standard input (redirect it with "<" from the
serial port, if you want) into the variable $line, but if in 4 seconds it
doesn't get a line, it gives up.
If you really want cat with a timeout, you can simulate one with a script.
For example (I did *not* test this):
#!/bin/sh
cat /dev/... &
pid=$!
sleep 5
kill $pid
But I'm not sure how exactly this makes sense (when reading from the serial
port, basically cat will never finish, and will always be killed exactly after
5 seconds - even if a line appeared after just 1 second).
--
Nadav Har'El | Thursday, Jun 10 2010, 28 Sivan 5770
nyh at math.technion.ac.il |-----------------------------------------
Phone +972-523-790466, ICQ 13349191 |Time is the best teacher. Unfortunately
http://nadav.harel.org.il |it kills all its students.
More information about the Linux-il
mailing list