Advertisement
FocusedWolf

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

Feb 26th, 2024 (edited)
1,607
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.48 KB | None | 0 0
  1. File: /usr/local/bin/yarr
  2. USAGE: $ yarr    <-- Update the mirrors list and then update the system with yip script.
  3.  
  4.     #!/bin/bash
  5.  
  6.     # Installation:
  7.     #     $ sudo cp ./yarr /usr/local/bin/
  8.     #     $ sudo chmod +x /usr/local/bin/yarr
  9.  
  10.     # If reflector is not installed.
  11.     if ! command -v reflector >/dev/null 2>&1
  12.     then
  13.         # NOTE: When installing Arch, the ISO's installation environment includes a copy of $ reflector to assist during install.
  14.         #       Its just not installed to the system by default.
  15.         echo -e "\e[94m::\e[0m Reflector could not be found. Install with: $ sudo pacman -S reflector"
  16.         exit
  17.     fi
  18.  
  19.     # Update /etc/pacman.d/mirrorlist with the 10 fastest HTTPS mirrors.
  20.     sudo reflector --verbose --country "United States" --latest 10 --protocol https --sort rate --save /etc/pacman.d/mirrorlist
  21.     echo
  22.  
  23.     # Call yip script.
  24.     yip
  25.  
  26. File: /usr/local/bin/yip
  27. USAGE: $ yip    <-- Update installed packages, delete downloaded packages, and purge the orphans, like a boss.
  28.  
  29.     #!/bin/bash
  30.  
  31.     # Installation:
  32.     #     $ sudo cp ./yip /usr/local/bin/
  33.     #     $ sudo chmod +x /usr/local/bin/yip
  34.  
  35.     free_space_check() {
  36.         local directory=$1
  37.         local low_space_mb=$2
  38.  
  39.         # If directory does not exist or is not a directory.
  40.         if [[ ! -d "$directory" ]]; then
  41.             echo -e "\e[31m::\e[0m ERROR: Directory '$directory' does not exist."
  42.             exit 1
  43.         fi
  44.  
  45.         # Get all space values in bytes.
  46.         local low_space_bytes=$(awk "BEGIN {print $low_space_mb * 1024 * 1024}") # Using awk because bash will overflow: $ echo $((9999999999999 * 1024 * 1024)) # will display -7960984073710600192.
  47.         local free_space_bytes=$(df --block-size=1 --output=avail "$directory" 2>/dev/null | tail -n1)
  48.         local total_space_bytes=$(df --block-size=1 --output=size "$directory" 2>/dev/null | tail -n1)
  49.  
  50.         # If free space is less than low space.
  51.         if awk -v free="$free_space_bytes" -v low="$low_space_bytes" 'BEGIN { exit !(free < low) }'; then
  52.             local si_scalars=(1208925819614629174706176 1180591620717411303424 1152921504606846976 1125899906842624 1099511627776 1073741824 1048576 1024)
  53.             local si_prefixes=("YiB" "ZiB" "EiB" "PiB" "TiB" "GiB" "MiB" "KiB")
  54.  
  55.             # Find the appropriate SI prefix.
  56.             local scalar=1
  57.             local prefix="KiB"
  58.             for i in ${!si_scalars[@]}; do
  59.                 if awk -v space="$total_space_bytes" -v scalar="${si_scalars[$i]}" 'BEGIN { exit !(space >= scalar) }'; then
  60.                     scalar="${si_scalars[$i]}"
  61.                     prefix="${si_prefixes[$i]}"
  62.                     break
  63.                 fi
  64.             done
  65.  
  66.             # Calculate output space info.
  67.             local free_space_scaled=$(awk -v bytes="$free_space_bytes" -v scalar="$scalar" 'BEGIN { printf "%.1f", bytes / scalar }')
  68.             local total_space_scaled=$(awk -v bytes="$total_space_bytes" -v scalar="$scalar" 'BEGIN { printf "%.1f", bytes / scalar }')
  69.             local used_percent=$(awk -v free="$free_space_bytes" -v total="$total_space_bytes" 'BEGIN { printf "%.f", (1 - free / total) * 100 }')
  70.  
  71.             while true; do
  72.                 # Displays disk space info like Dolphin properties window.
  73.                 echo -e "\e[31m:: WARNING: Low disk space detected. ${free_space_scaled} ${prefix} free of ${total_space_scaled} ${prefix} (${used_percent}% used) on drive '${directory}'."
  74.                 echo -e "            This can cause incomplete installations, system crashes, or render the system unbootable.\e[0m"
  75.                 echo
  76.                 read -p "Do you want to continue? [Y/N]: " response
  77.                 # Sets response to 'Y' if you didn't press Y|N, so Enter allows you to continue like it does with Yay/Pacman.
  78.                 response=${response:-Y}
  79.                 case "$response" in
  80.                     y|Y) echo; break ;;
  81.                     n|N) exit 1 ;; # Exit script with status 1 to indicate failure.
  82.                     *) echo; echo "Please answer Y or N."; echo ;;
  83.                 esac
  84.             done
  85.         fi
  86.     }
  87.  
  88.     # WIP low-space thresholds. Feel free to mod.
  89.     # If paths are on the same drive then you can remove redundant checks.
  90.     free_space_check /boot/efi 50
  91.     free_space_check /boot     500
  92.     free_space_check /         10240
  93.     free_space_check "$HOME"   10240
  94.  
  95.     # Update packages including AUR.
  96.     yay -Syyu
  97.     # I actually use this one:
  98.     # yay -Syyu --noconfirm
  99.  
  100.     # Remove unneeded dependencies.
  101.     # 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.
  102.     #
  103.     # NOTE: The command [yay -Ycc --noconfirm] is implemented in https://github.com/Jguer/yay/blob/next/cmd.go
  104.     #       It calls cleanDependencies(...) defined in https://github.com/Jguer/yay/blob/next/clean.go
  105.     #       Which calls hangingPackages(...) defined in https://github.com/Jguer/yay/blob/next/query.go
  106.     #
  107.     # WARNING: This version will not work [$ yes | yay -Scc --noconfirm].
  108.     #          The prompt will still be displayed to delete dependencies,
  109.     #          Yay AUR build directory $HOME/.cache/yay/ will be purged,
  110.     #          but no cleaning will take place in Pacman cache directory /var/cache/pacman/pkg/ and Pacman database directory /var/lib/pacman/
  111.     #          because the --noconfirm forces the "yes |" to be ignored, resulting in the default Pacman behavior of not cleaning anything to occur.
  112.     echo -e "\e[94m::\e[0m Remove unneeded dependencies..."
  113.     yay -Ycc --noconfirm
  114.  
  115.     # Clean Pacman cache directory /var/cache/pacman/pkg/, Pacman database directory /var/lib/pacman/, and Yay AUR build directory $HOME/.cache/yay/.
  116.     # If you need to downgrade or install an outdated package then you will need to download it again.
  117.     #
  118.     # NOTE: To just clean the Pacman directories the command is [$ yes | sudo pacman -Scc].
  119.     # WARNING: This version doesn't do anything: [$ sudo pacman -Scc --noconfirm]
  120.     #          SEE: https://bbs.archlinux.org/viewtopic.php?id=236186
  121.     #          SEE: https://unix.stackexchange.com/questions/52277/pacman-option-to-assume-yes-to-every-question
  122.     # WARNING: This command is broken as well [$ yay -Scc --noconfirm]. It will only clean Yay AUR build directory: $HOME/.cache/yay/
  123.     echo -e "\e[94m::\e[0m Clean Pacman and Yay caches..."
  124.     yes | yay -Scc
  125.     echo
  126.  
  127.     # Purge the orphans.
  128.     # NOTE: This code might be redundant because of [yay -Ycc --noconfirm]. I've yet to see it actually find an orphan.
  129.     orphans=$(pacman -Qdtq)
  130.     if [[ -n "$orphans" ]]; then
  131.         echo -e "\e[94m::\e[0m Remove orphaned packages..."
  132.         sudo pacman -Rns $orphans
  133.     else
  134.         echo -e "\e[94m::\e[0m No orphaned packages found..."
  135.     fi
  136.  
  137.     # Update gvim plugins.
  138.     # Assumes vim-plug is installed: https://github.com/junegunn/vim-plug
  139.     echo
  140.     echo -e "\e[94m::\e[0m Updating vim plugins..."
  141.     # Update ~/.vim/ plugins.
  142.     gvim +PlugUpgrade +PlugUpdate +qall
  143.     # Update /root/.vim/ plugins.
  144.     # NOTE: I have GVIM_ENABLE_WAYLAND=1 in my /etc/environment so gvim is resizable in Wayland.
  145.     # -E is needed so the environment variables are available to the sudo instance of gvim.
  146.     # -H is needed so $HOME=/root instead of ~/home/wolf (a side effect of using the -E option).
  147.     sudo -EH gvim +PlugUpgrade +PlugUpdate +qall
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement