💾 Archived View for gemini.cyberbot.space › smolzine › termduck.sh captured on 2022-01-08 at 14:07:23.

View Raw

More Information

-=-=-=-=-=-=-

#!/bin/bash

echo "Catch the duckies! When a duck appears, press ENTER as fast as possible.
Aim for a low average time to capture. Ctrl+C to quit."
total_time=0
total_duckies=0

while true; do
	sleep $((RANDOM%120+1))
	duck_start=$(date +%s)
	echo "  -._.-._.-. \_o< --QUACK!"
	read
	duck_time=$(($(date +%s)-duck_start))
	((total_duckies+=1))
	((total_time+=duck_time))
	# This is a bit of a neat trick. Bash only does integer arithmetics. Therefore we first get the number of seconds in the average, and then the extant number of hundredths of seconds, and print that as a faux floating point number.
	echo "Yay you caught it! Your average is now $((total_time/total_duckies)).$((100*total_time/total_duckies%100)) seconds.\n"
done