[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: shell script: How to determine if I'm in the background?



On 28 Aug 2002 11:58:39 +0200 Jaume Guasch <jaume.guasch@psi.ch> wrote:

> This snip is from the info page (Bash -> Interactive Shells -> Is this
> Shell Interactive? ): 
> 
> Jaume Guasch <jaume.guasch=QlBsmsa8rgI@public.gmane.org> writes:
> > 
> > "To determine within a startup script whether or not Bash is running
> > interactively, test the value of the `-' special parameter.  It
> > contains `i' when the shell is interactive.  For example:
> > 
> >      case "$-" in
> >      *i*)	echo This shell is interactive ;;
> >      *)	echo This shell is not interactive ;;
> >      esac
> > "

That was one of the first solutions I've come across in my web search,
and is, in fact, mentioned in quite a few web pages, like
http://www.tldp.org/LDP/abs/html/intandnonint.html

On those pages there is, however, a warning: "Scripts may be forced to
run in interactive mode with the -i option or with a #!/bin/bash -i
header. Be aware that this can cause erratic script behavior or show
error messages even when no error is present".

I certainly don't want my scripts to exhibit erratic behaviour. Besides,
I'm not sure how the concept of "interactive" relates to the concept of
"background", and I dont like the idea of trusting a shell variable that
may or may not exist in shells other than bash. If a process can be
backgrounded on all sorts of unix systems running all kinds of shells,
then that process should be able to determine if it's in the background
in all those circumstances.

-- 
Carlos Sousa
http://vbc.dyndns.org/



Reply to: