Advertisement
FocusedWolf

Arch: yip and yarp pacman+yay scripts to update like a boss

Feb 26th, 2024 (edited)
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.60 KB | None | 0 0
  1. File: /usr/local/bin/yarp
  2. USAGE: $ yarp    <-- Update the mirrors list and then update the system with yip script.
  3.  
  4.     #!/bin/bash
  5.  
  6.     # Installation:
  7.     #     $ sudo cp ./yarp /usr/local/bin/
  8.     #     $ sudo chmod +x /usr/local/bin/yarp
  9.  
  10.     # Check if reflector is installed.
  11.     if ! command -v reflector >/dev/null 2>&1
  12.     then
  13.         echo -e "\e[94m::\e[0m Reflector could not be found. Install with: $ sudo pacman -S reflector"
  14.         exit
  15.     fi
  16.  
  17.     # Retrieve the latest mirror list from the Arch Linux Mirror Status page, filter the most up-to-date mirrors, sort them by speed, and overwrite the file /etc/pacman.d/mirrorlist.
  18.     # NOTE: When installing Arch, the ISO's installation environment includes a copy of reflector to assist during install. Its just not installed to the system by default.
  19.     sudo reflector --verbose --country "United States" --latest 10 --protocol https --sort rate --save /etc/pacman.d/mirrorlist
  20.     echo
  21.  
  22.     # Call yip script.
  23.     yip
  24.  
  25. File: /usr/local/bin/yip
  26. USAGE: $ yip    <-- Update installed packages, delete downloaded packages, and purge the orphans, like a boss.
  27.  
  28.     #!/bin/bash
  29.  
  30.     # Installation:
  31.     #     $ sudo cp ./yip /usr/local/bin/
  32.     #     $ sudo chmod +x /usr/local/bin/yip
  33.  
  34.     free_space_check() {
  35.         local directory=$1
  36.         local low_space=$2
  37.  
  38.         # Check if directory exists.
  39.         if [[ ! -d "$directory" ]]; then
  40.             echo -e "\e[31m::\e[0m ERROR: Directory '$directory' does not exist."
  41.             exit 1
  42.         fi
  43.  
  44.         # Get available and total space in megabytes.
  45.         local available_space=$(df --output=avail "$directory" 2>/dev/null | tail -n1 | awk '{print int($1/1024)}')
  46.         local total_space=$(df --output=size "$directory" 2>/dev/null | tail -n1 | awk '{print int($1/1024)}')
  47.  
  48.         # Calculate percentage of free space.
  49.         local free_percent=$((available_space / total_space * 100))
  50.  
  51.         # Check if disk space is below threshold.
  52.         if [[ "$available_space" -lt "$low_space" ]]; then
  53.             while true; do
  54.                 echo -e "\e[31m:: WARNING: Low disk space detected. ${free_percent}% free (${available_space} MB) on drive '${directory}'."
  55.                 echo -e "            Running out of disk space during an update could corrupt your system.\e[0m"
  56.                 echo
  57.                 read -p "Do you want to continue? [Y/N]: " response
  58.                 case "$response" in
  59.                     y|Y) echo; break ;;
  60.                     n|N) exit 1 ;; # Exit script with status 1 to indicate failure.
  61.                     *) echo; echo "Please answer Y or N."; echo ;;
  62.                 esac
  63.             done
  64.         fi
  65.     }
  66.  
  67.     # WIP values. Feel free to mod.
  68.     free_space_check /boot/efi 50
  69.     free_space_check /boot     500
  70.     free_space_check /         10240
  71.     free_space_check "$HOME"   10240
  72.  
  73.     # Update packages including AUR.
  74.     yay -Syyu
  75.     # I actually use this one:
  76.     # yay -Syyu --noconfirm
  77.  
  78.     # Remove unneeded dependencies.
  79.     # NOTE: Strangely both [$ yes | yay -Scc] and [$ yay -Scc --noconfirm] will prompt you to remove dependencies so [yay -Ycc --noconfirm] needs to be called first to prevent that prompt from displaying.
  80.     #
  81.     # NOTE: The command [yay -Ycc --noconfirm] is implemented in https://github.com/Jguer/yay/blob/next/cmd.go
  82.     #       It calls cleanDependencies(...) defined in https://github.com/Jguer/yay/blob/next/clean.go
  83.     #       Which calls hangingPackages(...) defined in https://github.com/Jguer/yay/blob/next/query.go
  84.     #
  85.     # WARNING: This version will not work [$ yes | yay -Scc --noconfirm].
  86.     #          The prompt will still be displayed to delete dependencies,
  87.     #          Yay AUR build directory $HOME/.cache/yay/ will be purged,
  88.     #          but no cleaning will take place in Pacman cache directory /var/cache/pacman/pkg/ and Pacman database directory /var/lib/pacman/
  89.     #          because the --noconfirm forces the "yes |" to be ignored, resulting in the default Pacman behavior of not cleaning anything to occur.
  90.     echo -e "\e[94m::\e[0m Remove unneeded dependencies..."
  91.     yay -Ycc --noconfirm
  92.  
  93.     # Clean Pacman cache directory /var/cache/pacman/pkg/, Pacman database directory /var/lib/pacman/, and Yay AUR build directory $HOME/.cache/yay/.
  94.     # If you need to downgrade or install an outdated package then you will need to download it again.
  95.     #
  96.     # NOTE: To just clean the Pacman directories the command is [$ yes | sudo pacman -Scc].
  97.     # WARNING: This version doesn't do anything: [$ sudo pacman -Scc --noconfirm]
  98.     #          SEE: https://bbs.archlinux.org/viewtopic.php?id=236186
  99.     #          SEE: https://unix.stackexchange.com/questions/52277/pacman-option-to-assume-yes-to-every-question
  100.     # WARNING: This command is broken as well [$ yay -Scc --noconfirm]. It will only clean Yay AUR build directory: $HOME/.cache/yay/
  101.     echo -e "\e[94m::\e[0m Clean Pacman and Yay caches..."
  102.     yes | yay -Scc
  103.     echo
  104.  
  105.     # Purge the orphans.
  106.     # NOTE: This code might be redundant because of [yay -Ycc --noconfirm]. I've yet to see it actually find an orphan.
  107.     orphans=$(pacman -Qdtq)
  108.     if [[ -n "$orphans" ]]; then
  109.         echo -e "\e[94m::\e[0m Remove orphaned packages..."
  110.         sudo pacman -Rns $orphans
  111.     else
  112.         echo -e "\e[94m::\e[0m No orphaned packages found..."
  113.     fi
  114.  
  115.     # Optional section for vim users.
  116.     # echo
  117.     # echo -e "\e[94m::\e[0m Updating vim plugins..."
  118.     # # Update ~/.vim/ plugins.
  119.     # gvim +PlugUpgrade +PlugUpdate +qall
  120.     # # Update /root/.vim/ plugins.
  121.     # sudo gvim +PlugUpgrade +PlugUpdate +qall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement