This is a follow-up question to a previous question regarding a bash script I wrote to help me more accurately keep track of my vices. I'm now trying to modify the script to display all non-zero units:
It's been 2 weeks, 1 day and 5 hours since you last bought a deck.
To that end here are the relevant parts of the script I have so far:
last_bought=$(cat "$lb_file") # file contains time in epoch seconds
...
now=$(date -u +%s)
elapsed="$((now-last_bought))"
W=$((elapsed/60/60/24/7))
D=$((elapsed/60/60/24))
H=$((elapsed/60/60%24))
if [[ $W -le 0 && $D -le 0 && $H -gt 0 ]]; then string="$H hours"
elif [[ $W -le 0 && $D -gt 0 && $H -gt 0 ]]; then string="$D days and $H hours"
elif [[ $W -gt 0 && $D -gt 0 && $H -le 0 ]]; then string="$W weeks and $D days"
elif [[ $W -gt 0 && $D -le 0 && $H -gt 0 ]]; then string="$W weeks and $H hours"
elif [[ $W -gt 0 && $D -gt 0 && $H -gt 0 ]]; then string="$W weeks, $D days and $H hours"
fi
This works perfectly for hours and days (running the script with an hours value of -b 50 shows 2 days and 2 hours). However, when an hour value is entered into the script that is at least one week, both the $W and $D variables are populated. This means that for a value of -b 168 (1 week in hours), the output will be:
It's been 1 weeks and 7 days since you last bought a deck.
How can I fix this, preferably while also implementing singular periods of time like so:
It's been 1 week and 1 hour since you last bought a deck.
I'm also aware that I'm probably checking my logic in a very crude way, so if there's a smarter/more common way to do so I'd appreciate being enlightened, and