-
Notifications
You must be signed in to change notification settings - Fork 13
/
.switch_shell
135 lines (114 loc) · 3.11 KB
/
.switch_shell
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/sh
#
# 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$
#
# Usage:
#
# . /path/to/.switch_shell [-d] [ /path/to/new_shell [ <new shell options> ]
# -d turns on debugging
# FIXME: move shell chooser code into .ns
# Give hook a .local suffix for consistency in case we need to exclude local stuff
PREFERRED_SH_HOOK_FILE=$HOME/.preferred_shell.local
# only switch if we're interactive
case "$-" in
*i*)
: ;; # OK
*)
echo "Warning: tried to switch a non-interactive shell"
return 1
;;
esac
switch_shell_debug () {
[ -n "$debug" ] || return 0
if [ -n "$_have_sh_load_status" ]; then
sh_load_status "$*\n"
else
echo "$*"
fi
}
switch_shell_safely () {
# we do this rather than exec() just in case $myshell fails to run.
switch_shell_debug "Switching to $myshell safely ..."
# if "$myshell" $myshell_args; then
# switch_shell_debug "$myshell exited OK; exiting parent shell."
# exit
# else
# switch_shell_debug "$myshell had exit code $?, back to pid $$"
# fi
# Very cute trick from Bart Schaefer which checks in advance whether
# we can switch shell. This is better than the above because it
# is not dependent on the subshell exiting with 0 status.
eval `$myshell -f -c "echo exec $myshell" || echo :` '$myshell_args'
}
switch_shell_dangerously () {
switch_shell_debug "Switching to $myshell dangerously ..."
exec "$myshell" $myshell_args
}
switch_shell () {
if [ ! -x $myshell ]; then
switch_shell_debug "$myshell not executable; aborting switch."
return
fi
if [ -n "$NO_SWITCH" ]; then
switch_shell_debug 'Shell switching disabled by $NO_SWITCH; aborting.'
return
fi
export SHELL_ARGS="$myshell_args" # no other way of retrieving these?
switch_shell_debug "Switching to $myshell, args: $myshell_args"
case "$SHLVL" in
"") # unknown, be careful
switch_shell_safely $myshell_args
;;
1) # login shell, be careful
switch_shell_safely $myshell_args
;;
force_danger|*) # other shell, be risky and save memory
switch_shell_dangerously $myshell_args
# switch_shell_safely $myshell_args
;;
esac
}
debug=
if [ "$1" = '-d' ]; then
debug=yes
shift
fi
# Sensible default shell to switch to.
myshell=`which zsh` 2>/dev/null
#myshell=
myshell_args=
if [ -n "$1" ]; then
# shell to switch to specified via ARGV
myshell="$1"
shift
myshell_args="$@"
else
# get preferred shell
if [ -e $PREFERRED_SH_HOOK_FILE ]; then
if grep '=' $PREFERRED_SH_HOOK_FILE >/dev/null 2>&1; then
. $PREFERRED_SH_HOOK_FILE
else
echo "$PREFERRED_SH_HOOK_FILE doesn't look like a shell script; aborting shell switch" >&2
return 0
fi
fi
fi
if [ -n "$ZDOTDIR" ]; then
. "$ZDOTDIR/.shared_env"
fi
if sh_load_status "testing if we have sh_load_status" >/dev/null 2>&1; then
_have_sh_load_status=1
else
_have_sh_load_status=
#echo "Warning: no sh_load_status" >&2
fi
if [ -z "$myshell" ]; then
echo "No shell preference found; not switching shell." >&2
return 0
fi
switch_shell $myshell_args