Last active
December 16, 2022 04:04
-
-
Save 103sbavert/d1f71d22b73373846e78dc11646d9c66 to your computer and use it in GitHub Desktop.
A shell script made by ChatGPT to update Arch Linux. This script takes five options, namely, -h, -c, -o, -a and -f. If provided none, -o, -a and -f are assumed (in that order). Options can be specified individually divided by spaces or they can be combined together like so: -aof. The order is preserved.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Define error messages | |
errmes[1]="Error: Unknown argument" | |
errmes[2]="Error: Too many arguments" | |
errmes[3]="Error: Unknown option" | |
# Define colors | |
GREEN="\e[32m" | |
RED="\e[31m" | |
NORMAL="\e[0m" | |
# Define update functions | |
function official_update { | |
echo -e "${GREEN}Updating with pacman...${NORMAL}" | |
sudo pacman -Syu | |
} | |
function aur_update { | |
echo -e "${GREEN}Looking for updates in the AUR...${NORMAL}" | |
yay -Syua | |
} | |
function firmware_update { | |
echo -e "${GREEN}Looking for firmware updates...${NORMAL}" | |
sudo fwupdmgr refresh --force && sudo fwupdmgr get-updates && sudo fwupdmgr update | |
} | |
function clean_cache { | |
echo -e "${GREEN}Cleaning downloaded packages...${NORMAL}" | |
yay -Scc | |
} | |
# Define update function | |
function update { | |
for (( i=0; i<${#1}; i++ )); do | |
c=${1:$i:1} | |
case $c in | |
o) | |
# Update system with pacman | |
official_update | |
if [ $? -ne 0 ]; then | |
echo -e "${RED}Error: pacman update failed${NORMAL}" | |
fi | |
;; | |
a) | |
# Check for updates in the AUR | |
aur_update | |
if [ $? -ne 0 ]; then | |
echo -e "${RED}Error: AUR update failed${NORMAL}" | |
fi | |
;; | |
f) | |
# Look for firmware updates | |
firmware_update | |
if [ $? -ne 0 ]; then | |
echo -e "${RED}Error: firmware update failed${NORMAL}" | |
fi | |
;; | |
c) | |
# Clean downloaded packages from cache | |
clean_cache | |
if [ $? -ne 0 ]; then | |
echo -e "${RED}Error: cleaning cache failed${NORMAL}" | |
fi | |
;; | |
*) | |
# Unknown option | |
echo -e "${RED}${errmes[3]} -$c${NORMAL}" | |
return 3 | |
esac | |
done | |
} | |
# Check for --help option | |
for var in "$@"; do | |
if [ "$var" == "--help" ]; then | |
echo "Usage: arch-update [OPTION]... [PACKAGE]..." | |
echo "Update system with pacman, look for firmware updates, and check for updates in the AUR." | |
echo "" | |
echo " -o update system with pacman" | |
echo " -f look for firmware updates" | |
echo " -a check for updates in the AUR" | |
echo " -c clean downloaded packages from cache" | |
echo "" | |
echo " --help display this help and exit" | |
exit 0 | |
fi | |
done | |
# Check number of arguments | |
if [ $# -eq 0 ]; then | |
official_update | |
aur_update | |
firmware_update | |
exit 0 | |
fi | |
# Check for single argument | |
if [ $# -eq 1 ]; then | |
update "${1:1}" | |
exit 0 | |
fi | |
# Check for multiple arguments | |
if [ $# -le 4 ]; then | |
for i in "$@"; do | |
update "${i:1}" | |
if [ $? -eq 3 ]; then | |
exit 3 | |
fi | |
done | |
else | |
echo -e "${RED}${errmes[2]}${NORMAL}" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment