#!/bin/sh cachefile="$HOME/.cache/weather" location="Saint-Petersburg" update_freq=1800 obsolete_after=10800 update_forecast() { # Check several times before giving up, useful when just waking up from sleep # since internet is usually not available right away wget -q --tries=5 --timeout=20 --spider 1.1.1.1 > /dev/null || exit forecast=$(curl -s wttr.in/"$location"?format=1) if [ ${#forecast} -lt 30 ]; then printf "%s\n" "$forecast" > "$cachefile" fi } if [ -f "$cachefile" ]; then last_modif=$(date +%s -r "$cachefile") current_secs=$(date +%s) elapsed=$((current_secs - last_modif)) if [ $elapsed -gt $obsolete_after ]; then rm "$cachefile" fi if [ $elapsed -gt $update_freq ]; then update_forecast fi else update_forecast fi if [ ! -f "$cachefile" ]; then exit fi cat "$cachefile"