Skip to content

Instantly share code, notes, and snippets.

@okurka12
Last active October 21, 2024 09:34
Show Gist options
  • Save okurka12/f00b18199ac17154e50ecad6866b3bd2 to your computer and use it in GitHub Desktop.
Save okurka12/f00b18199ac17154e50ecad6866b3bd2 to your computer and use it in GitHub Desktop.
retrieve & update my .bashrc or .bash_aliases
#!/bin/sh
ALIASES_URL=https://gist.githubusercontent.com/okurka12/cc3769db8e6f57dfdf091428095b7e00/raw
ALIASES_BACKUP=.bash_aliases.old
ALIASES_NEW=.bash_aliases.new
# ask if something is ok, exit if it's not
# if OPTION_Y variable is "-y", do nothing
prompt_confirm () {
if [ "$OPTION_Y" != "-y" ]; then
printf "%s (y/n) " "$*"
read -r PROCEED
if [ "$PROCEED" != "y" ]; then
echo "abort"
exit
fi
fi
}
# if $1 exists, ask if its ok to delete and then delete
safe_delete () {
if [ -f "$1" ]; then
prompt_confirm "delete $1?"
echo "deleting $1"
rm "$1"
fi
}
# if $1 exists, ask if its ok to overwrite
ask_overwrite () {
if [ -f "$1" ]; then
prompt_confirm "overwrite $1?"
fi
}
################################################################################
if [ "$1" != "aliases" ] && [ "$1" != "rc" ]; then
echo "USAGE: $0 aliases|rc [-y]"
exit
fi
OPTION_Y="$2"
GO_BACK_TO=$(pwd)
cd "$HOME" || exit
if [ "$1" = "aliases" ]; then
ask_overwrite "$ALIASES_BACKUP"
cp .bash_aliases "$ALIASES_BACKUP"
safe_delete "$ALIASES_NEW"
wget -O "$ALIASES_NEW" "$ALIASES_URL"
echo "DIFFERENCES:"
diff .bash_aliases $ALIASES_NEW
prompt_confirm "continue?"
cp "$ALIASES_NEW" .bash_aliases
rm "$ALIASES_NEW"
echo "done"
echo "- old .bash_aliases can be found in $ALIASES_BACKUP"
echo "- don't forget to source .bash_aliases or restart bash"
fi
if [ "$1" = "rc" ]; then
echo "retrieving .bashrc is not implemented yet"
fi
cd "$GO_BACK_TO" || exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment