Skip to content

Instantly share code, notes, and snippets.

@ormaaj
Last active May 21, 2022 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ormaaj/a86e6c9d0ef18a990f41fa863a3e5228 to your computer and use it in GitHub Desktop.
Save ormaaj/a86e6c9d0ef18a990f41fa863a3e5228 to your computer and use it in GitHub Desktop.
#!/bin/bash
if [[ ${KSH_VERSION+${!KSH_VERSION}} == .sh.version ]]; then
typeset -n FUNCNAME=.sh.fun
if builtin pids 2>/dev/null; then
function BASHPID.get { .sh.value=$(pids -f '%(pid)d'); }
elif [[ -r /proc/self/stat ]]; then
function BASHPID.get { read -r .sh.value _ </proc/self/stat; }
else
function BASHPID.get { .sh.value=$(exec sh -c 'echo $PPID'); }
fi
else
shopt -s extglob lastpipe
fi
function libinit {
unset -f "${FUNCNAME[0]}"
{ alias libinit=$(</dev/fd/3); } 3<<-\EOF
\shopt -s extglob lastpipe expand_aliases
\shopt -u assoc_expand_once
\set +m
\source -- "${rootPath%+(/)}/aliases"
setupGlobalAliases
EOF
typeset uid euid
if
[[
$UID != +([[:digit:]]) ||
"UID != (uid = $(id -ur))" -ne 0 ||
$EUID != +([[:digit:]]) ||
"EUID != (euid = $(id -u))" -ne 0
]]
then
typeset -a args
shopt -q login_shell && args+=(--login)
shopt -qo posix && args+=(--posix)
shopt -qo xtrace && args+=(-x)
[[ $- == *i* ]] && args+=(-i)
args+=(-s)
typeset x
x=${BASHPID:-$$} x=$(readlink -sne "/proc/${x}/exe") || x=/bin/bash
exec -- "$(type -P env)" -- "UID=${uid}" "EUID=${euid}" "$x" "${args[@]}" -- "$@"
fi
if rootPath=$(dirname "$(readlink -sne -- "${BASH_SOURCE[0]:-$0}")") &&
dotFilesPath=$(readlink -sne -- "${rootPath}/../../..") &&
fsRoot=$(readlink -sne -- "${dotFilesPath}/..") &&
[[ -d $rootPath && -d $dotFilesPath && -d $fsRoot ]]
then
export rootPath dotFilesPath fsRoot
else
echo 'Failed to locate relative library paths.' >&2
return 1
fi
}
libinit
libinit
# vim: set ft=bash fenc=utf-8 ff=unix ts=4 noet :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment