Skip to content

Instantly share code, notes, and snippets.

@jessebutryn
Created December 26, 2019 21:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jessebutryn/61d1ca16a8a8837fee4f9475dc26b86a to your computer and use it in GitHub Desktop.
Save jessebutryn/61d1ca16a8a8837fee4f9475dc26b86a to your computer and use it in GitHub Desktop.
bash script to convert text to morse code
#!/usr/bin/env bash
#
#set -x
#######################################
declare -A morse
morse[0]='- - - - -'
morse[1]='. - - - -'
morse[2]='. . - - -'
morse[3]='. . . - -'
morse[4]='. . . . -'
morse[5]='. . . . .'
morse[6]='- . . . .'
morse[7]='- - . . .'
morse[8]='- - - . .'
morse[9]='- - - - .'
morse[A]='. -'
morse[B]='- . . .'
morse[C]='- . - .'
morse[D]='- . .'
morse[E]='.'
morse[F]='. . - .'
morse[G]='- - .'
morse[H]='. . . .'
morse[I]='. .'
morse[J]='. - - -'
morse[K]='- . -'
morse[L]='. - . .'
morse[M]='- -'
morse[N]='- .'
morse[O]='- - -'
morse[P]='. - - .'
morse[Q]='- - . -'
morse[R]='. - .'
morse[S]='. . .'
morse[T]='-'
morse[U]='. . -'
morse[V]='. . . -'
morse[W]='. - -'
morse[X]='- . . -'
morse[Y]='- . - -'
morse[Z]='- - . .'
#######################################
while read -rN1 c; do
c=${c^}
if [[ $c == $'\n' ]]; then
printf '\n'
elif [[ $c == ' ' ]]; then
printf ' '
else
printf '%s ' "${morse[$c]}"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment