Testing for a script that is waiting on stdin

It would really help if you were a lot more specific about your script and/or command. But in case what you want to do is test where stdin is coming from, this example script will demonstrate that for you:

#!/bin/bash
if [[ -p /dev/stdin ]]
then
    echo "stdin is coming from a pipe"
fi
if [[ -t 0 ]]
then
    echo "stdin is coming from the terminal"
fi
if [[ ! -t 0 && ! -p /dev/stdin ]]
then
    echo "stdin is redirected"
fi
read
echo "$REPLY"

Example runs:

$ echo "hi" | ./demo
stdin is coming from a pipe
$ ./demo
[press ctrl-d]
stdin is coming from the terminal
$ ./demo < inputfile
stdin is redirected
$ ./demo <<< hello
stdin is redirected
$ ./demo <<EOF
goodbye
EOF
stdin is redirected

Without knowing what you're trying to do I'd argue you should be writing the script so that you always know if it's going to ask for stdin or not. Either that or pipe something into the command that's likely to be wanting something from stdin, but that's probably not the best idea.


If you're on Linux you could use strace to see if the process is trying to read from stdin. Other platforms have similar programs (e.g. dtrace, ktrace, or truss).

You might be able to avoid the issue altogether by feeding the output of yes to the command in question.