How to I test if a script is run from and interactive shell?
by mfoley from LinuxQuestions.org on (#5J1ET)
I have a script that can be run as a sendmail alias command, or a cron job, or run by a user. I'd like the script to know if it's being run by cron or otherwise in the background versus a user running the command at a bash prompt.
What I've found so far on the web says that if $PS1 exists, it's running from a user command line. However, I created a simple script:
Code:echo "$PS1" >/tmp/interand after running it from the command line /tmp/inter is empty. I've also read that $- will have an "i", so similar script:
Code:echo $- >/tmp/interThat gives "hB", no "i".
So, how do I do this?


What I've found so far on the web says that if $PS1 exists, it's running from a user command line. However, I created a simple script:
Code:echo "$PS1" >/tmp/interand after running it from the command line /tmp/inter is empty. I've also read that $- will have an "i", so similar script:
Code:echo $- >/tmp/interThat gives "hB", no "i".
So, how do I do this?