On my system (probably on yours too) you can test for pipe input by looking into /proc/$$/fd/0 - which is the standard input (stdin) file descriptor symlink.
Here's an example script and three examples of it in use:
Code:
#!/bin/sh
# file: example.sh
readlink /proc/$$/fd/0
Code:
bash$ ./example.sh
/dev/pts/0
bash$ ls | ./example.sh
pipe:[27112838]
bash$ ./example.sh < input.txt
input.txt
Also, if you want to read the input that was piped into the script, you can simply use cat to read it from the pipe.
Code:
#!/bin/sh
if readlink /proc/$$/fd/0 | grep -q "^pipe:"; then
echo "Pipe input:"
elif file $( readlink /proc/$$/fd/0 ) | grep -q "character special"; then
echo "Standard input:"
else
echo "File input:"
fi
cat # read from stdin and write to stdout