Ideally, this would be scriptable in shell, but Perl or Python would be fine. C code could be helpful, but probably fails cost/benefit.
I recognize that redirection to a
In Python:
import sys print sys.stdout.isatty()
Examples:
$ ./a True $ ./a | less False