Created
May 6, 2020 11:07
-
-
Save anekos/c34e896ed347dc343d6c8f4f3c3df223 to your computer and use it in GitHub Desktop.
Tally counter (Press ESC to exit)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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