#!/bin/bash # Check for pause time. pause_time=0 if [[ "$1" ]]; then if [[ $1 =~ ^([0-9]+):([0-9]+):([0-9]+)$ ]]; then pause_time=$(((${BASH_REMATCH[1]}*60*60) + (${BASH_REMATCH[2]}*60) + (${BASH_REMATCH[3]}))) elif [[ $1 =~ ^([0-9]+):([0-9]+)$ ]]; then pause_time=$(((${BASH_REMATCH[1]}*60) + (${BASH_REMATCH[2]}))) elif [[ $1 =~ ^([0-9]+)$ ]]; then pause_time=$1 else echo "Bad pause time: $1" >&2 exit 1 fi fi # Get national debt. function get_debt() { local t=$(wget --quiet http://brillig.com/debt_clock -O - | grep debtiv.gif) t=$(sed -e 's/.*ALT="\$//' -e 's/".*//' -e 's/[ ,]//g' <<<$t) echo $t } # Print time item. # Pass time value and one of 'hour', 'minute', 'ssecond'. function fmt_time() { local t=$1 local p=$2 if [[ $t -gt 0 ]]; then if [[ $t -eq 1 ]]; then printf '%d %s' $t $p else printf '%d %ss' $t $p fi fi } # Print the elapsed time between the first and second argument. # Times given in seconds. # Earliest time is first arugment. function elapsed_time() { local st=$1 local et=$2 local dt=$((et - st)) local ds=$((dt % 60)) local dm=$(((dt / 60) % 60)) local dh=$((dt / 3600)) echo $(fmt_time $dh 'hour') $(fmt_time $dm 'minute') $(fmt_time $ds 'second') } ###################################################################### stime=$(date +%s) sdebt=$(get_debt) if [[ $pause_time -gt 0 ]]; then sleep $pause_time; fi etime=$(date +%s) edebt=$(get_debt) t=$(elapsed_time $stime $etime) d=$(bc <<<"scale=2; $edebt - $sdebt") printf "During the last %s the US National Debt has increased by %s\n" "$t" "$d" ## vim: tabstop=4: shiftwidth=4: noexpandtab: ## kate: tab-width 4; indent-width 4; replace-tabs false;