Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tally counter (Press ESC to exit)
#!/bin/bash
set -euC
# set -o pipefail
declare -A counts
function result () {
stty "$stty"
for i in "${!counts[@]}"
do
printf "%s\t%d\n" "$i" "${counts[$i]}"
done | sort
}
trap result EXIT
stty="$(stty -g)"
stty -echo
while read -rn 1 ch
do
# Press `Esc` to exit
[ "$ch" = $'\x1b' ] && exit 0
[ -z "$ch" ] && ch='??'
counts["$ch"]="$(( ${counts["$ch"]:-0} + 1 ))"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment