Re: Standard .bashrc needs a minor fix

2010-05-08 Thread Joachim Schmitz

Greg Wooledge wrote:

On Thu, May 06, 2010 at 09:30:20AM -0500, Chuck Remes wrote:

The standard .bashrc contains a line of code that precludes certain
scripts from executing. It has to do with the logic for checking if
the session is interactive. 


There's no such thing as a "standard .bashrc", at least not from the
point of view of a bash bug mailing list.  Files like .bashrc are
supplied by the OS vendor, or created by the user.


e.g.
[ -z "$PS1" ] && return


That's certainly *not* how I'd write that check.  If the goal is to
protect a block of commands from running when the shell is invoked
without a terminal, then I'd prefer this:

if [ -t 1 ]; then
   # All your terminal commands go here
   stty kill ^u susp ^z intr ^c
   ...
fi


I'm doing it like this:

it [[ ${-#*i} = $- || ! -t 0 ]]
   # non-interactive stuff here
   ...
else
   # interactive stuff here
   stty ...
   ...
fi

bye, Jojo




Re: Standard .bashrc needs a minor fix

2010-05-08 Thread Ken Irving
On Fri, May 07, 2010 at 03:03:57PM -0400, Mike Frysinger wrote:
> On Friday 07 May 2010 08:49:26 Greg Wooledge wrote:
> > On Thu, May 06, 2010 at 09:30:20AM -0500, Chuck Remes wrote:
> > > e.g.
> > > [ -z "$PS1" ] && return
> > 
> > That's certainly *not* how I'd write that check.  If the goal is to
> > protect a block of commands from running when the shell is invoked
> > without a terminal, then I'd prefer this:
> > 
> > if [ -t 1 ]; then
> > # All your terminal commands go here
> > stty kill ^u susp ^z intr ^c
> > ...
> > fi
> 
> the somewhat common test ive seen in different distros to detect interactive 
> shells is:
> if [[ $- != *i* ]] ; then
>   # shell is non-interactive
>   return
> fi
> -mike

bash(1) (v4.1) includes:

OPTIONS
...
-iIf the -i option is present, the shell is interactive.

and 

INVOCATION
...
PS1 is set and $- includes i if bash is interactive,  allowing
a shell script or a startup file to test this state.
 
The latter statement is ambiguous, possibly suggesting that PS1 should
also be checked to test for an interactive shell.  This is apparently
not the case, judging by this and other responses in this thread.  I'm 
not sure how to reword it, but if testing for i in $- is sufficient then
this might be clarified.

Ken
-- 
ken.irv...@alaska.edu