[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: read -t 0 anomaly
From: |
Pierre Gaston |
Subject: |
Re: read -t 0 anomaly |
Date: |
Fri, 4 Oct 2013 14:51:00 +0300 |
On Fri, Oct 4, 2013 at 2:20 PM, Kunszt Árpád <
arpad.kunszt@syrius-software.hu> wrote:
> I tried to use "read -t 0" to check if there is any data on the STDIN or
> not.
>
> The man page said:
>
> If timeout is 0, read returns success if input is available on the
> specified file descriptor, failure otherwise.
>
> Maybe I made a mistake but I tested and I got variable results:
>
> arpad@terminus ~ $ for(( i=0; i<10; i++ )); do echo -n "a" | read -t 0 ;
> echo $?; done | sort | uniq -c
> 10 0
> arpad@terminus ~ $ for(( i=0; i<10; i++ )); do echo -n "a" | read -t 0 ;
> echo $?; done | sort | uniq -c
> 8 0
> 2 1
> arpad@terminus ~ $ for(( i=0; i<10; i++ )); do echo -n "a" | read -t 0 ;
> echo $?; done | sort | uniq -c
> 10 0
>
> I tried this on 2 machines with the same results:
>
> GNU bash, version 4.1.5(1)-release (x86_64-pc-linux-gnu)
> GNU bash, 4.2.45(1)-release (x86_64-pc-linux-gnu) verzió
>
>
> Am I doing something wrong? Did I misunderstand the documentation? Or is
> there a race condition?
>
> Thanks,
>
> Arpad Kunszt
There is a race condition, you cannot know if echo will run before read.