Friday, December 21, 2018

A Quick bash Morse Code Generator

I've gotten into the habit of scribbling down the standard Morse code alphabet on the last page of my pocket notepad:

I don't have a specific reason for this practice, I was just curious if it would come in handy. And as a matter of fact, earlier this week it did.

While visiting our nieces and nephew in Boston, we had a discussion about the Titanic that turned into a discussion about issuing an SOS, which in turn brought us around to Morse Code. Later in the evening, I found myself sitting with Dovid with a few minutes to kill while Shira and his Dad went through some diabetic maintenance tasks. I pulled out my notepad and gave Dovid a quick tutorial on how to encode and decode letters using Morse code. We took turns writing out a few short messages, and quickly ran out of time. I promised him that the next day I'd send him a longer message over e-mail for him to decode.

The next day, as I found myself thinking about the message I was going to send him, it occurred to me just how painful hand-translating the text was going to be. The programmer in me kicked in and I quickly whipped up the script below to map text to dots and dashes. Using it, I was able to do the following:

$ smorse 'Dovid, you rock!'
-.. --- ...- .. -.. , / -.-- --- ..-  / .-. --- -.-. -.- !

$ (echo "Dovid," ; echo "We had fun" ; echo "visiting with you" ; echo "yesterday") |smorse 
-.. --- ...- .. -.. ,
.-- .  / .... .- -..  / ..-.. ..- -. 
...- .. ... .. - .. -. --.  / .-- .. - ....  / -.-- --- ..- 
-.-- . ... - . .-. -.. .- -.-- 

While the command is noticeably slow, I'm impressed with how it came out. It's certainly much faster than hand translation.

Here's the code for the command. Who knows, maybe you too can use it to impress your niece or nephew.

#!/bin/bash                                                                                                                                                                                                         

##                                                                                                                                                                                                                  
## Very Simple Morse Code Translator                                                                                                                                                                                
##                                                                                                                                                                                                                  


input=$HOME/.smorse.input.$$
output=$HOME/.smorse.output.$$

mapping=$(cat <<EOF                                                                                                                                                                                           
a:.-                                                                                                                                                                                                                
b:-...                                                                                                                                                                                                              
c:-.-.                                                                                                                                                                                                              
d:-..                                                                                                                                                                                                               
e:.                                                                                                                                                                                                                 
f:..-..                                                                                                                                                                                                             
g:--.                                                                                                                                                                                                               
h:....                                                                                                                                                                                                              
i:..                                                                                                                                                                                                                
j:.---                                                                                                                                                                                                              
k:-.-                                                                                                                                                                                                               
l:.-..                                                                                                                                                                                                              
m:--                                                                                                                                                                                                                
n:-.                                                                                                                                                                                                                
o:---                                                                                                                                                                                                               
p:.--.                                                                                                                                                                                                              
q:--.-                                                                                                                                                                                                              
r:.-.                                                                                                                                                                                                               
s:...                                                                                                                                                                                                               
t:-                                                                                                                                                                                                                 
u:..-                                                                                                                                                                                                               
v:...-                                                                                                                                                                                                              
w:.--                                                                                                                                                                                                               
x:-..-                                                                                                                                                                                                              
y:-.--                                                                                                                                                                                                              
z:--..                                                                                                                                                                                                              
EOF                                                                                                                                                                                                                 
)

if [ -z "$*" ] ; then
  cat > $input
else
  echo "$@" > $input
fi

tr '[A-Z]' '[a-z]' < $input > $output
mv $output $input

sed 's| | / |g' < $input > $output
mv $output $input

for pair in  $mapping ; do
  letter=$(echo $pair | cut -d : -f 1)
  code=$(echo $pair | cut -d : -f 2)
  sed "s/$letter/$code /g" < $input > $output
  mv $output $input
 done

cat $input

No comments:

Post a Comment