zsh-workers
 help / color / mirror / code / Atom feed
* Switching shell safely and efficiently
@ 2001-09-07 18:35 Adam Spiers
  2001-09-07 19:05 ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Adam Spiers @ 2001-09-07 18:35 UTC (permalink / raw)
  To: zsh workers mailing list

Regarding switching shells if zsh isn't your login shell ...

I spawn new shells often enough that a safety-check prompt as
suggested in section 1.7 of the FAQ would be too annoying.  Using exec
is preferable, to avoid leaving loads of copies of bash lying around
in the process table doing nothing, but for me, the exec has been
known to fail (despite a -x check succeeding) due to library
mismatches between our various machines in the office.  So I wanted
something that hit the sweet spot in the safety/efficiency trade-off,
and I came up with the following.  Feel free to criticise, but if
enough people think it's ok, would it be worth including in Misc/, or
mentioning in the FAQ?

--------- 8< --------- 8< --------- 8< --------- 8< --------- 8< ---------
# Adam's .switch_shell
#
# Try switch shell if we're interactive, aiming for safety, but
# not so much that we end up hogging memory.
#
# $Id: .switch_shell,v 1.9 2001/09/07 18:34:01 adam Exp $
#
# Usage:
#
# . /path/to/.switch_shell [-d] [ /path/to/new_shell [ <new shell options> ]
#    -d turns on debugging

if [ "$1" = '-d' ]; then
  debug=yes
  shift
fi

if [ -n "$1" ]; then
  myshell="$1"
  shift
else
  # Sensible default shell to switch to.
  myshell=`which zsh`
fi

myshell_args="$@"

switch_shell_safely () {
  # we do this rather than exec() just in case $myshell fails to run.
  [ -n "$debug" ] && echo "Switching to $myshell safely ..."
  $myshell $myshell_args "$@" && exit
}

switch_shell_dangerously () {
  [ -n "$debug" ] && echo "Switching to $myshell dangerously ..."
  exec $myshell $myshell_args "$@"
}

switch_shell () {
  if [ ! -x $myshell ]; then
    [ -n "$debug" ] && echo "$myshell not executable; aborting switch."
    return
  fi

  if [ -n "$NO_SWITCH" ]; then
    [ -n "$debug" ] && echo 'Shell switching disabled by $NO_SWITCH; aborting.'
    return
  fi

  case "$SHLVL" in
    1) # login shell, be careful
       switch_shell_safely "$@"
       ;;
    *) # other shell, be risky and save memory
       switch_shell_dangerously "$@"
       ;;
  esac
}

# only switch if we're interactive
case "$-" in
  *i*) switch_shell "$@"
       ;;
esac
--------- 8< --------- 8< --------- 8< --------- 8< --------- 8< ---------

Then you put something like

  [ -e ~/.switch_shell ] && . ~/.switch_shell

in your .bashrc.  Although I wrote it to be as portable as possible
(how global is $SHLVL ?), one obvious disadvantage is that it won't
work with any type of csh.

You also need

  bash () {
    NO_SWITCH="yes" command bash "$@"
  }

somewhere in your .zshrc if you're ever forc^Wrequired to invoke bash.

Adam


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Switching shell safely and efficiently
  2001-09-07 18:35 Switching shell safely and efficiently Adam Spiers
@ 2001-09-07 19:05 ` Bart Schaefer
  2001-09-07 22:45   ` Adam Spiers
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2001-09-07 19:05 UTC (permalink / raw)
  To: Adam Spiers, zsh-workers

On Sep 7,  7:35pm, Adam Spiers wrote:
} 
} Using exec is preferable, to avoid leaving loads of copies of bash
} lying around in the process table doing nothing, but for me, the exec
} has been known to fail (despite a -x check succeeding) due to library
} mismatches between our various machines in the office. So I wanted
} something that hit the sweet spot in the safety/efficiency trade-off

    run_shell=`$myshell -c "echo exec $myshell" || echo :`
    eval $run_shell '"$@"'

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Switching shell safely and efficiently
  2001-09-07 22:45   ` Adam Spiers
@ 2001-09-07 22:28     ` Bart Schaefer
  2001-09-08  2:18       ` Adam Spiers
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2001-09-07 22:28 UTC (permalink / raw)
  To: Adam Spiers, zsh-workers

On Sep 7, 11:45pm, Adam Spiers wrote:
> Subject: Re: Switching shell safely and efficiently
> Bart Schaefer (schaefer@brasslantern.com) wrote:
> >     run_shell=`$myshell -c "echo exec $myshell" || echo :`
> >     eval $run_shell '"$@"'

Now that I think about it, that could be just:

  eval `$myshell -c "echo exec $myshell" || echo :` '"$@"'

> more stylish than my attempt, it's not quite as efficient through
> having to fork the shell before running it properly.  To this end, a
> slight improvement might be skipping start-up files

I knew you were going to say that.

I chose to omit the -f in case the contents of the startup files are what
causes the shell to fail.  You did want safety ...


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Switching shell safely and efficiently
  2001-09-07 19:05 ` Bart Schaefer
@ 2001-09-07 22:45   ` Adam Spiers
  2001-09-07 22:28     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Adam Spiers @ 2001-09-07 22:45 UTC (permalink / raw)
  To: zsh-workers

Bart Schaefer (schaefer@brasslantern.com) wrote:
>     run_shell=`$myshell -c "echo exec $myshell" || echo :`
>     eval $run_shell '"$@"'

Cute trick!  Nice enough to put in the FAQ, I think.  Although much
more stylish than my attempt, it's not quite as efficient through
having to fork the shell before running it properly.  To this end, a
slight improvement might be skipping start-up files:

  run_shell=`$myshell -f -c "echo exec $myshell" || echo :`
  eval $run_shell '"$@"'

I haven't done any benchmarking of the different approaches though.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Switching shell safely and efficiently
  2001-09-07 22:28     ` Bart Schaefer
@ 2001-09-08  2:18       ` Adam Spiers
  0 siblings, 0 replies; 5+ messages in thread
From: Adam Spiers @ 2001-09-08  2:18 UTC (permalink / raw)
  To: zsh-workers

Bart Schaefer (schaefer@brasslantern.com) wrote:
> On Sep 7, 11:45pm, Adam Spiers wrote:
> > more stylish than my attempt, it's not quite as efficient through
> > having to fork the shell before running it properly.  To this end, a
> > slight improvement might be skipping start-up files
> 
> I knew you were going to say that.
> 
> I chose to omit the -f in case the contents of the startup files are what
> causes the shell to fail.  You did want safety ...

Bah, you're too clever ;-)


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-09-08  1:37 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-09-07 18:35 Switching shell safely and efficiently Adam Spiers
2001-09-07 19:05 ` Bart Schaefer
2001-09-07 22:45   ` Adam Spiers
2001-09-07 22:28     ` Bart Schaefer
2001-09-08  2:18       ` Adam Spiers

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).