Pages

Generate Morse Code from ASCII

The Morse code, named after its inventor Samuel Morse, was used for long distance communication.
It is largely believed that it is now obsolete, yet some prisoners still find it useful. (banging a shoe on a pipe)
Learn more about it at Wikipedia.

Anyway here is the script
#!/bin/bash
# morse.sh - generate morse code from ascii
# you can contact me at:
# --. .-. ..- .-.. --- ... .--.-. --. -- .- .. .-.. 
# .-.-.- -.-. --- --

# if you don't like my "dots" and "dashes"
# you can change them
# dot=.
# dash=_

# generate ascii (from space to ~)
for ((i=32; i<127; i++)); do
ascii="$ascii$(echo -en $(printf "%s%o" "\\" "$i"))"
done

# english chars that can be translated to morse
eng=".,?'!/():;=-\"\$@ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

# The morse code for each character in eng
mor=( # punctuation
·-·-·- --··-- ··--·· ·----· -·-·-- -··-· -·--·- -·--·-
---··· -·-·-· -···- -····- ·-··-·
···-··- ·--·-·
# alphabet A-Z
·- -··· -·-· -·· · ··-· --· ···· ·· ·--- -·- ·-·· -- -·
--- ·--· --·- ·-· ··· - ··- ···- ·-- -··- -·-- --··
# Numbers 0-9
----- ·---- ··--- ···-- ····-
····· -···· --··· ---·· ----· )

mor=( ${mor[@]//·/${dot:-·}} )
mor=( ${mor[@]//-/${dash:--}} )

while read -a line; do
for i in "${line[@]}"; do
for (( j=0; j<${#i}; j++ )); do

if  [ "${eng/${i:$j:1}/}" == "$eng" ]; then
pos=${ascii%%${i:$j:1}*}
eval char=\$\'\\$( printf "%o" "$(( ${#pos} ))" )\'
[ "${eng/$char/}" == "$eng" ] && continue
else
char="${i:$j:1}"
fi

pos="${eng%%$char*}"
printf "%s " "${mor[${#pos}]}"

done
printf " "
done
printf "\n"
done

~$ ls /|./morse.sh
~$ ./morse.sh</dev/urandom

No comments: