Look at this code:

while read -t 3 line; do
    echo ${line}
done < /opt/data.log

data.log is a fifo. One process is writing to this, and this code should be reading it to show to the user. But I want it to exit when there is no more data in the fifo for 3 seconds.

The problem is it never times out. I write to this fifo, it shows me:

read: 6: Illegal option -t 

What is wrong?


Yes, I was using /bin/sh as a shebang which is a symbolic link to dash. I changed it to /bin/bash. but it does not make any benefit. It waits for ever if no one opens the fifo. I want a timeout solution that works when the other end of the fifo has not opened it.

  • Perhaps useful: unix.stackexchange.com/questions/10698/… But if you don't have stringent portability requirements like I did, using bash is a lot simpler – Gilles Jul 25 '12 at 22:48
  • The timeout isn't happening because the read isn't executing yet. The redirection has to open the FIFO before the loop can start, and the open is blocking. You need a timeout on the redirection operator. I can't think of a good way to do that... – Alan Curry Jul 28 '12 at 5:20

are you using bash? or some other sh? read -t is a kshism (also supported by bash and zsh but not standard)

It works in bash, but not in dash:

$ bash -c 'echo foo | while read -t 3 xxxx ; do echo $xxxx ; done'
$ dash -c 'echo foo | while read -t 3 xxxx ; do echo $xxxx ; done'
dash: 1: read: Illegal option -t
  • yes. I'm using bash in debian 6.0.5 – Majid Azimi Jul 25 '12 at 9:59
  • I think you're using dash or some other minimalist sh. – cas Jul 25 '12 at 10:02
  • 3
    @MajidAzimi what's your shebang line? If it is #!/bin/sh you are not using bash but dash – Ulrich Dangel Jul 25 '12 at 11:43
  • your are right, I was using dash. but it doesn't make any difference. see the update. – Majid Azimi Jul 28 '12 at 4:37

I did find a good way to do this:

exec 5<>/opt/data.log

while read -7 3 line <& 5; do
    echo ${line}

I open it for read and write so the script does not block for ever. here is the descriptin: https://stackoverflow.com/a/4875924/654269

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.