In Bash, I would like to use /dev/stdin. As with many commands, stdin is optional though. So, how would I know if I had stdin to read? If there is no standard input, a command like cat /dev/stdin hangs waiting for input.
Here is my first attempt:
#Always says "no stdin"
if test -s /dev/stdin
then
echo stdin
else
echo no stdin
fi
man test => -s FILE exists and has a size greater than zero
I guess the device file does not keep up with the available bytes. It would be nice to do this in bash, ssh implements this functionality (in another language no doubt).
I think you are looking for Asynchronous IO.
Try read with
-tflag. It will return after specified time even if there was no input to be read.