Skip to content

Instantly share code, notes, and snippets.

@skgsergio
Last active December 6, 2022 10:33
Show Gist options
  • Select an option

  • Save skgsergio/8e41483a46393e836a4c to your computer and use it in GitHub Desktop.

Select an option

Save skgsergio/8e41483a46393e836a4c to your computer and use it in GitHub Desktop.
Script for updating an Arch Linux system using reflector for mirror updates, yay as package manager and printing pacnew files.
#!/bin/bash
#
# System update script for Arch Linux.
#
# Requires:
# - yay as package manager.
# - reflector as mirror list updater.
#
# Sergio Conde <skgsergio@gmail.com>
#
## Style vars
styleReset="\e[0m"
styleBold="\e[1m"
colorStatus="\e[32m"
colorCommand="\e[35m"
colorQuestion="\e[33m"
colorError="\e[31m"
colorReset="\e[39m"
## Functions
statusEcho() { echo -e "${styleBold}${colorStatus}[*] ${colorReset}$@${styleReset}"; }
commandEcho() { echo -e "${styleBold}${colorCommand}[>] ${colorReset}$@${styleReset}"; }
questionEcho() { echo -ne "${styleBold}${colorQuestion}[?] ${colorReset}$@${styleReset}"; }
errorEcho() { echo -e "${styleBold}${colorError}[!] ${colorReset}$@${styleReset}"; }
# doCmd defaultRunYesNo Command to execute
doCmd() {
defaultRun=${1:0:1}; shift
commandEcho "$@"
questionEcho '\tRun it? '$([[ $defaultRun == 'y' ]] && echo '[Y/n]' || echo '[y/N]')' '
read -r -n 1 -s response
if [[ $response == '' ]]; then response=$defaultRun; fi
echo $response
if [[ ${response,,} == 'y' ]]; then
$@
exitCode=$?
if [[ $exitCode -ne 0 ]]; then errorEcho "\"$@\" failed with code $exitCode"; fi
fi
echo
[[ ${response,,} == 'y' ]]; return $?
}
## Update mirror list
statusEcho 'Updating mirror list'
doCmd n sudo reflector -l 10 -n 5 -p https --sort rate --save /etc/pacman.d/mirrorlist
if [[ $? -eq 0 ]]; then
grep "^Server =" /etc/pacman.d/mirrorlist
echo
fi
## Update repo packages (pacman)
statusEcho 'Updating packages'
doCmd y yay
## Search for new config files
statusEcho 'Searching for new config files'
newFiles=$(sudo find /etc -iname '*.pacnew' -or -iname '*.pacsave')
numFiles=$(echo $newFiles | wc -w)
if [ $numFiles -gt 0 ]; then
commandEcho "There are $numFiles new configuration file(s):"
for newFile in $newFiles; do
commandEcho "\t$newFile"
done
else
commandEcho 'No new configuration files found'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment