Skip to content

Instantly share code, notes, and snippets.

@anekos
Created May 6, 2020 11:07
Show Gist options
  • Save anekos/c34e896ed347dc343d6c8f4f3c3df223 to your computer and use it in GitHub Desktop.
Save anekos/c34e896ed347dc343d6c8f4f3c3df223 to your computer and use it in GitHub Desktop.
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