[Tutor] interactif or not

ZIYAD A. M. AL-BATLY zamb at saudi.net.sa
Sat Jun 4 01:32:25 CEST 2005

On Fri, 2005-06-03 at 18:45 +0100, Alan G wrote:
> > If I invoke it in a shell.. then it can be verbose
> > 
> > If it is launched from a crontab.. then it is less verbose.
> You need to check who the process owner is.
> That can be done on *Nix by reading the USER environment 
> variable. Cron jobs are usually run under the 'cron' user
> I believe.
> Hoever that won;t work if the python script is executed 
> in a shell script that is then executed by a user. Its usually 
> better to make verbosity controllable by a flag 
> - traditionally -v. Thus the default is non verbode and 
> verbosity can be switched on(or even given any one of 
> several levels) as desired.
> Alan G.
I think there's a better solution.  Here's a quote from the “bash”
        “An interactive shell is one started without non-option
        arguments and without the -c option whose standard input and
        error are        both connected to terminals (as determined by
        isatty(3)), or one started with the -i option.  PS1 is set and
        $- includes i if bash is interactive, allowing a shell script or
        a startup file to test this state.”

>From above you can examine the “$-” environment variable and check for
“i” in it, if it exist you have an interactive “bash” shell, if not you
have a non-interactive “bash” shell.  However this only works if you're
using “bash” (or, maybe, compatible shells like “ksh”, but I'm not

The other option would be to check for the environment variable “$PS1”.
If it exist, then it's most likely an interactive shell (and I think it
works with other shells like “csh”, again I'm not sure as I never worked
with any other shell but “bash”).

Hope this helpful to someone.

More information about the Tutor mailing list