Article 5J1ET How to I test if a script is run from and interactive shell?

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?latest?d=yIl2AUoC8zA latest?i=vSOI0ugsDp0:ycXEodZmM-Y:F7zBnMy latest?i=vSOI0ugsDp0:ycXEodZmM-Y:V_sGLiP latest?d=qj6IDK7rITs latest?i=vSOI0ugsDp0:ycXEodZmM-Y:gIN9vFwvSOI0ugsDp0
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments