In section "6.3.2 Is this Shell Interactive?", the Bash Manual advocates:
if [ -z "$PS1" ]; then
echo This shell is not interactive
fi
however, this does not work in any shell I've tested (RedHat 4 & 5,
OpenSUSE 11.1, SLES 10 & 11). It seems that the idea of testing the
prompt variable was taken from section 5.5 of the (now) historical
Unix F.A.Q. originally written in 1992.
In current Bash implementations, the "PS" variables are not set when
invoking bash scripts interactively (I believe they are only set for
login shells). Try this mini-script:
---
#!/bin/bash
set | grep PS
---
Even without the "#!/bin/bash", it still returns the same result.
However, "man bash" has a different idea, the "-t fd" test as in:
if [[ -t 0 ]] ; then
echo interactive
fi
Good except that interactive scripts aren't always attached to a
terminal. Sometimes they are attached to a socket. So:
if [[ -t 0 || -S /dev/stdin ]] ; then
echo interactive
fi
Of course, just because stdin in bound to a socket doesn't prove that
it's interactive since you could invoke it remotely with cron but
presumably the author of the script would be aware of it's intended use.
Regards,
--
John Lange
----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.