Implement sleep without reading from stdin of the caller

Also modified to return success to the caller and remove the distraction of a
default value for timeout.

Based on https://blog.dhampir.no/content/sleeping-without-a-subprocess-in-bash-and-how-to-sleep-forever
Archived version: https://archive.fo/OHbV1
This commit is contained in:
Amir Yalon 2019-07-02 12:40:35 +03:00
parent e3e73ba302
commit 3256f5c71f
1 changed files with 13 additions and 1 deletions

View File

@ -1913,7 +1913,7 @@ Surprisingly, `sleep` is an external command and not a `bash` built-in.
read_sleep() {
# Usage: sleep 1
# sleep 0.2
read -rst "${1:-1}" -N 999
read -rt "$1" <> <(:) || :
}
```
@ -1925,6 +1925,18 @@ read_sleep 0.1
read_sleep 30
```
For performance-critical situations, where it is not economic to open and close an excessive number of file descriptors, the allocation of a file descriptor may be done only once for all invocations of `read`:
(See the generic original implementation at https://blog.dhampir.no/content/sleeping-without-a-subprocess-in-bash-and-how-to-sleep-forever)
```shell
exec {sleep_fd}<> <(:)
while some_quick_test; do
# equivalent of sleep 0.001
read -t 0.001 -u $sleep_fd
done
```
## Check if a program is in the user's PATH
```shell