Skip to content

Instantly share code, notes, and snippets.

@lululock71
Forked from logarytm/update-notifier.sh
Last active August 19, 2021 08:37
Show Gist options
  • Save lululock71/0d1185b8d7a6231edf4a01f03712b74a to your computer and use it in GitHub Desktop.
Save lululock71/0d1185b8d7a6231edf4a01f03712b74a to your computer and use it in GitHub Desktop.
package update notifications for Arch Linux, easily portable to other distros
#!/usr/bin/env bash
# Requires "pacman-contrib"
# Configuration
cache_file=$HOME/.updates
# function to check updates, must work without root privileges and print
# the number of updates to stdout.
function update_count {
checkupdates | wc -l; return $?
}
# notification function, called: notify TOTAL [NEW]
function notify {
if [ $# -eq 2 ]; then
notify-send --icon=info "$2 new updates ($1 total)" "Run pacman -Syu to upgrade"
else
notify-send --icon=info "$1 updates available" "Run pacman -Syu to upgrade"
fi
}
# Test for network conection
function online_check {
for interface in $(ls /sys/class/net/ | grep -v lo);
do
online=$(cat /sys/class/net/$interface/carrier)
if [ "$online" = 1 ]; then
echo 1
fi
done
}
# main app
online=$(online_check)
# Retry in one minute because my laptop is so slow (okay, not that slow) to connect to a wifi network...
if [ "$online" != 1 ]; then
notify-send --icon=info "Arch Linux package update notifier" "Waiting internet connection... Will retry in one minute."
sleep 1m
online=$(online_check)
fi
if [ "$online" != 1 ]; then
notify-send --icon=error "Arch Linux package update notifier" "Update check failed ! Check your internet connection !"
else
count=$(update_count)
fi
if [ -f "$cache_file" ] && [ $(cat $cache_file) -ge 0 ]; then
last_count=$(cat $cache_file)
if [ $((count+last_count)) = 0 ]; then
notify-send --icon=info "No package update available..." "How rare is that ?"
elif [ "$last_count" = "$count" ]; then
notify $count
elif [ $last_count -lt $count ]; then
notify $count $((count-last_count))
echo $count > $cache_file
fi
elif [ $count -ge 0 ]; then
notify $count
echo $count > $cache_file
fi
@lululock71
Copy link
Author

I added a function to test if a internet connection is up. My laptop takes some time to connect to my wi-fi network and the script would have been launched before the connection is up...

Thanks to the original script, made by logarytm.

Also thanks to the persons who answered on this Stackoverflow thread .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment