Skip to content

Instantly share code, notes, and snippets.

@GrayedFox
Last active September 26, 2023 01:47
Show Gist options
  • Save GrayedFox/6c303647551e6f4081f44c9eeb9c4836 to your computer and use it in GitHub Desktop.
Save GrayedFox/6c303647551e6f4081f44c9eeb9c4836 to your computer and use it in GitHub Desktop.
Daily tasks that should be run every morning. Colour codes output for readability; can also fetch latest changes and tries to automatically rebase active branches.
#!/usr/bin/env bash
isNumber() {
( printf "%f" $1 >/dev/null 2>&1 ) \
&& echo "true" \
|| echo "false"
}
log() {
GREEN='\033[0;32m'
RED='\033[0;31m'
BLUE='\033[0;34m'
PLAIN='\033[0m'
# colour blue for custom log messsages when no exit code passed
if [ $(isNumber $1) == "false" ] && [ "$#" -eq 1 ]; then
echo -e "${BLUE}$1${PLAIN}\n"
fi
# colour green for successful commands (exit code 0)
if [ $(isNumber $1) == "true" ] && [ "$1" -eq 0 ] && [ "$#" -eq 2 ]; then
echo -e "${GREEN}$2${PLAIN}\n"
fi
# colour red for failed commands (exit code 1)
if [ $(isNumber $1) == "true" ] && [ "$1" -eq 1 ] && [ "$#" -eq 2 ]; then
echo -e "${RED}$2${PLAIN}\n"
fi
}
refreshRepo() {
cd $1 &&
LAST_BRANCH=$(git symbolic-ref --short HEAD) &&
git checkout main &&
git pull
log $? "Got latest changes for $1"
git checkout $LAST_BRANCH &&
git rebase main
log $? "Switched back to $LAST_BRANCH and rebased with latest changes"
}
## replace following with system package manager i.e. brew/cask upgrade, etc
sudo apt update --yes
log $? "Apps updated..."
sudo apt upgrade --yes
log $? "Apps upgraded..."
sudo apt autoclean --yes
log $? "Apps autocleaned..."
sudo apt autoremove --purge --yes
log $? "Apps autoremoved..."
sudo snap refresh
log $? "Snap apps refreshed"
# set active repo/work directory
WORK_DIR="$HOME/Repos/Work"
refreshRepo "$WORK_DIR/first-app"
refreshRepo "$WORK_DIR/second-app"
log "Good morning Foxy!"
@GrayedFox
Copy link
Author

GrayedFox commented Aug 6, 2018

Includes some helpers to colour output messages and do some argument checking

@GrayedFox
Copy link
Author

GrayedFox commented Oct 13, 2018

No longer chains aptitude calls so error messages are colour coded based on each individual command exit status

@GrayedFox
Copy link
Author

Updated to use apt instead of apt-get, comment out example cleanUp command usage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment