2

I am very novice with scripting and I cannot figure this out. I'm trying to know if there is something to read and if not then

Paulo Tomé
  • 3,782
user388398
  • 23
  • 2
  • 2
    See https://unix.stackexchange.com/q/173779/77928. Do read -t 0 ... to set timeout to zero, and return immediately if there is nothing to read. – user000001 Dec 31 '19 at 11:53

1 Answers1

3

With bash specifically, read -t0 will return true if there's something to read or on stdin or the end of input has been reached and false otherwise.

if read -t0; then
  echo "there's something to be read on stdin, or end-of-file is reached"
else
  echo "there's nothing that may be read from stdin at the moment"
fi

Note that it will return true even if what's there to be read is not a full line or even a full character, and so a subsequent read may still hang waiting for an unescaped line delimiter.

If stdin is in non-blocking mode or if stdin is not readable, read -t0 will always return true.