Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
1,137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.79 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This is a simple script that refines the output of checkupdates and
  4. # grabs the latest news from the Arch RSS news feed. The idea is to
  5. # have some extra info before committing to grabbing a fresh copy
  6. # of the package database via pacman -Sy.
  7. #
  8. # Prerequisites: bc, xmllint
  9.  
  10. printf "Fetching data, please wait...\n\n"
  11.  
  12. CULIST=$(checkupdates)
  13. if [[ $CULIST = "" ]]; then
  14.     echo "No new packages"
  15.     exit
  16. fi
  17.  
  18. # calculate the number of packages
  19. IFS=$'\n'
  20. for i in $CULIST
  21. do
  22.     pacnum=$((pacnum+1))
  23. done
  24. echo "$pacnum new packages found"
  25.  
  26. # extract package names and then feed them to pacman -Si
  27. PNAMES=""
  28. for i in $CULIST
  29. do
  30.     PNAMES+=$(printf "%s" "$i" | awk '{print $1}')" "
  31. done
  32. IFS=''
  33. PSI="pacman -Si $PNAMES"
  34. PSI="$(eval "$PSI")"
  35.  
  36. # extract the relevant info from pacman -Si
  37. PSI=$(echo "$PSI" | grep -E 'Repository|Download Size')
  38. PSI=$(echo "$PSI" | \
  39.       awk -F ": " '{if (NR%2 == 0) printf "%s\n", $2; else printf "%s ", $2}')
  40.  
  41. # combine pacman -Si info with the output of checkupdates
  42. OUT=$(paste <(echo "$PSI") <(echo "$CULIST") | column -t | tr -s " ")
  43.  
  44. # calculate total download size and refine the output further
  45. IFS=$'\n'
  46. totsize_mb="0"
  47. for i in $OUT
  48. do
  49.     cursize_bib=$(echo "$i" | cut -d ' ' -f 2,3)
  50.  
  51.     # generate appropriate conversion multipliers
  52.     if [ "$(echo "$cursize_bib" | awk '{print $2}')" = "KiB" ]; then
  53.         mul=0.001024
  54.     else # assumes MiB
  55.         mul=1.048576
  56.     fi
  57.  
  58.     cursize_mb=$(echo "scale=1;($(echo "$cursize_bib" | \
  59.                 awk '{print $1}')*$mul)" | bc)
  60.     totsize_mb=$(echo "scale=1;$totsize_mb+$cursize_mb" | bc)
  61. done
  62.  
  63. # final output for the checkupdate stage
  64. IFS=''
  65. printf "\n%s\n" "$(echo -e "$OUT" | cut -d ' ' --complement -f 2,3 | \
  66.        sort -d | column -t )"
  67. printf "\nTotal download size: %.2f MB or %.2f MiB\n" \
  68.        "$totsize_mb" "$(echo "$totsize_mb"*0.953674 | bc)"
  69.  
  70. ###############################################################################
  71.  
  72. # arch news
  73. printf "\n==============================\n"
  74. printf "https://www.archlinux.org/news\n"
  75. printf "==============================\n"
  76.  
  77. # nicked from some forum, not an ideal solution, but seems to work,
  78. # formatting has been a little bit improved here
  79. curl -s https://www.archlinux.org/feeds/news/ | \
  80. xmllint --xpath //item/title\ \|\ //item/pubDate /dev/stdin | \
  81. sed -r -e "s:<title>([^<]*?)</title><pubDate>([^<]*?)</pubDate>:\2 -- \1\n:g" | \
  82. sed -r "s:&gt;:>:" | \
  83. sed -r "s:&lt;:<:" | \
  84. tr -s " " | \
  85. cut -d " " --complement -f 1,5,6 | \
  86. head -n5 # 5 of the most recent news items seems reasonable?
  87.  
  88. # syu prompt
  89. printf "\nLaunch sudo pacman -Syu? (y/N) "
  90. read -r CONT
  91. if [ "$CONT" = "y" ] || [ "$CONT" = "Y" ]; then
  92.     printf "\n"
  93.     sudo pacman -Syu
  94. else
  95.     printf "\nUpdate cancelled.\n"
  96. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement