the 'fgetc' function will not block on standard input but will attempt to read and return ch = -1 if there was no input waiting.
Bash has the builtin "read" command, which hangs on stdin until it can read some characters (or until it times out, if you set it to read with the -t option), but there seems to be no real way to imitate the C behavior described above. Is there?
Here is my implementation based on your stty idea and some modifications:
#!/bin/bash
if [ -t 0 ]; then
stty -echo -icanon time 0 min 0
fi;
read line
echo "The input was, if any: " $line
if [ -t 0 ]; then
stty sane
fi;
if that code was saved in an executable named test.sh, then you could do the following:
bash$ ./test.sh
The input was, if any:
bash$ echo "test" | ./test.sh
The input was, if any: test
The reason I'm using the if [ -t 0 ]; statement is to make sure that standard input is coming from the terminal, not a pipe, otherwise you'll get annoying errors:
Unfortunately none of the approaches above work unless a terminal or a pipe is present, so if you have the aformentioned test.sh on a remote machine, then:
bash$ echo "test" | ssh remote_machine ./test.sh
test
bash$ ssh -t remote_machine ./test.sh <== using -t flag to force terminal creation
bash$ ssh remote_machine ./test.sh
<== it blocks here on the read