-
Notifications
You must be signed in to change notification settings - Fork 2
/
.shellrc_dispatch
executable file
·84 lines (71 loc) · 2.62 KB
/
.shellrc_dispatch
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
#!/bin/bash
# Launch different bash configuration for Linux vs OSX, interactive vs batch
#
# More info at https://github.com/josephwecker/bashrc_dispatch
#
# License: Public Domain.
# Author: Joseph Wecker, 2012
# Configuration
# -------------
#
# EXPORT_FUNCTIONS: export SHELL_PLATFORM and shell_is_* functions for use
# in other scripts.
EXPORT_FUNCTIONS=""
[ -n "$BASH" ] && EXPORT_FUNCTIONS="true"
# Code
# ----
# Avoid recursive invocation
[ -n "$SHELL_DISPATCH_PID" ] && [ $$ -eq "$SHELL_DISPATCH_PID" ] && return
SHELL_DISPATCH_PID=$$
# Setup the main shell variables and functions
if [ -z "$SHELL_PLATFORM" ]; then
SHELL_PLATFORM='OTHER'
case "$OSTYPE" in
*'linux'* ) SHELL_PLATFORM='LINUX' ;;
*'darwin'* ) SHELL_PLATFORM='OSX' ;;
*'freebsd'* ) SHELL_PLATFORM='BSD' ;;
esac
fi
if ! type shell_is_login >/dev/null 2>&1; then
shell_is_linux () { [[ "$OSTYPE" == *'linux'* ]] ; }
shell_is_osx () { [[ "$OSTYPE" == *'darwin'* ]] ; }
shell_is_login () { if [ -n "$BASH" ]; then shopt -q login_shell; else [[ -o login ]]; fi }
shell_is_interactive () { test -n "$PS1" ; }
shell_is_script () { ! shell_is_interactive ; }
fi
shell_dispatch_brief() {
#echo "Brief: $@"
true
}
# Make $BASH_ENV the same in interactive and non-interactive scripts
[ -z "$BASH_ENV" ] && export BASH_ENV="$BASH_SOURCE"
# Make these available to the potentially convoluted shell_* startup scripts
if [ -n "$EXPORT_FUNCTIONS" ]; then
export SHELL_PLATFORM
export -f shell_is_linux
export -f shell_is_osx
export -f shell_is_login
export -f shell_is_interactive
export -f shell_is_script
fi
# Now dispatch special files
PRF="${HOME}/.shellrc"
[ -f "${PRF}_once" ] && [ -z "$BRCD_RANONCE" ] && shell_dispatch_brief "exe_once" && . "${PRF}_once" && export BRCD_RANONCE=true
[ -f "${PRF}_all" ] && shell_dispatch_brief "exe_all" && . "${PRF}_all"
[ -f "${PRF}_script" ] && shell_is_script && shell_dispatch_brief "exe_script" && . "${PRF}_script"
[ -f "${PRF}_interactive" ] && shell_is_interactive && shell_dispatch_brief "exe_interative" && . "${PRF}_interactive"
[ -f "${PRF}_login" ] && shell_is_login && shell_dispatch_brief "exe_login" && . "${PRF}_login"
# Unset variables if necessary to avoid env polution
if ! $EXPORT_FUNCTIONS ; then
unset SHELL_PLATFORM
unset -f shell_is_linux
unset -f shell_is_osx
unset -f shell_is_login
unset -f shell_is_interactive
unset -f shell_is_script
fi
# Unset local variables
unset fn_cmd
unset EXPORT_FUNCTIONS
unset SHELL_DISPATCH_PID
unset PRF