bashrc: checking if the environment is loaded before executing stuff that relies on it

This commit is contained in:
Gregor Bückendorf 2018-12-13 17:15:32 +01:00
parent abae11bfaf
commit 6b7f34f7f8
2 changed files with 47 additions and 5 deletions

48
.bashrc
View file

@ -9,11 +9,49 @@ if (( BASH_VERSINFO[0] < 4 )); then
echo "Please get a newer version of Bash." echo "Please get a newer version of Bash."
echo "Your bashrc won't work otherwise." echo "Your bashrc won't work otherwise."
else else
# Source .bashrc.d
for file in ~/.bashrc.d/*.bashrc; do
. "$file"
done
unset -v file # Basic safe boolean evaluation. See this Gist for details:
# https://gist.github.com/gliech/184dc7566821442202f21dfe15e2b7ff
function truthy {
if [[ "${1,,}" == @(y|yes|on|true|1) ]]; then
return 0
fi
return 1
}
function falsy {
if [[ "${1,,}" == @(n|no|off|false|0) ]]; then
return 0
fi
return 1
}
function true_false_default {
case "${1,,}" in
y | yes | on | true | 1)
return 0 ;;
n | no | off | false | 0)
return 1 ;;
esac
shift
eval "$@"
return 1
}
export -f truthy falsy true_false_default
if truthy $DOTFILES_ACTIVE; then
# Source .bashrc.d
for file in ~/.bashrc.d/*.bashrc; do
. "$file"
done
unset -v file
elif [[ ! -v DOTFILES_ACTIVE ]]; then
echo "Dotfiles environment not found."
echo "You should probably reload your login session before proceeding."
fi
fi fi

View file

@ -0,0 +1,4 @@
DOTFILES_ACTIVE=true
DOTFILES_DIR="${HOME}/.dotfiles"
DOTFILES_GITDIR="${HOME}/.dotgit"