Operating System - HP-UX
1834187 Members
2657 Online
110064 Solutions
New Discussion

Re: Checking if 'we' are running in an interactive shell....

 
SOLVED
Go to solution
Charles Harris
Super Advisor

Checking if 'we' are running in an interactive shell....

Dear all,

Just a quick question, I used to have a script floating about that could determin if a shell was interactive or not (ie run by cron etc). Please would some kind soul remind me how it's done?

All pointers / tips / RTFF etc warmly received as ever!

Cheers,

-=ChaZ=-
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: Checking if 'we' are running in an interactive shell....

Hi Charles:

# [ -t 0 ] && echo "Interactive"

Regards!

...JRF...
Jonathan Fife
Honored Contributor

Re: Checking if 'we' are running in an interactive shell....

For ksh I use:

if [[ $- = *i* ]]; then
#interactive shell
else
#non-interactive shell
fi
Decay is inherent in all compounded things. Strive on with diligence
spex
Honored Contributor

Re: Checking if 'we' are running in an interactive shell....

# typeset INTERACTIVE=0 && [ -t 0 ] && INTERACTIVE=1
A. Clay Stephenson
Acclaimed Contributor

Re: Checking if 'we' are running in an interactive shell....

The standard test for this is to determine if stdin (file descriptor 0) is associated with a tty (terminal-like) device so:

if [ -t 0 ]
then
echo "We are interactive"
else
echo "We ain't no interactive active session nohow"
fi

or better (because the external test command is replaced with the shell's internal test):

if [[ -t 0 ]]
then
echo "We are interactive"
else
echo "We ain't no interactive active session nohow"
fi
-------------------------------
In any event, testing to see if stdin is a tty device is a standard UNIX idiom.

If it ain't broke, I can fix that.
Charles Harris
Super Advisor

Re: Checking if 'we' are running in an interactive shell....

Thanks guys, I knew I was having a 'moment' it's still Monday here after all!!

Cheers!

-=ChaZ=-
Bill Hassell
Honored Contributor

Re: Checking if 'we' are running in an interactive shell....

Actually, I like this:

if tty -s
then
...interactive stuff...
fi

Highly portable among POSIX shells like ksh, HP's sh, bash, etc since an external command is used to determine the interactive status.


Bill Hassell, sysadmin