-t 0, which does correctly detect input from terminal or file.
I think the key here is knowing whether your input is coming from a terminal or from a file. There is a test for this (
man test, see
Assuming you're running a bash script:
if [ -t 0 ]; then
echo "Input from terminal"
if [ $# -eq 0 ]; then
echo "No input files specified on command line. Error." >&2
echo "Input file given on command line. It is $1"
echo "Input coming from stdin"
You can handle the different scenarios by substituting actual code in for the echo statements above.
Update, quickie test script:
[ -t 0 ] && echo "t is true" || echo "t is false"
$ test.sh testfile
t is true
$ test.sh < testfile
t is false