'Why does my Morse code converter display no output?

I want to make a morse code converter that converts letters from a txt file to morse code and morse code from a txt file to letters.

The way of execution is ./morse.sh -e Text.txt for encoding and ./morse.sh -d Morse_text.txt for decoding. My only problem is that the output is not displayed on the screen for some reason.

Here is the code. If I made a mistake let me know.

#!/bin/bash

echo "Hello,welcome to the Morse Code Converter!!"

encode(){
    declare -A morse
    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]="--..";
    morse[1]=".----";
    morse[2]="..---";
    morse[3]="...--";
    morse[4]="....-";
    morse[5]=".....";
    morse[6]="-....";
    morse[7]="--...";
    morse[8]="---..";
    morse[9]="----.";
    morse[0]="-----";

    for (( k = 0; k < ${#morse}; k=k + 1 )); do
        echo "${morse}"
    done
}

decode(){
    declare -A letter
    letter[.-]="A";
    letter[-...]="B";
    letter[-.-.]="C";
    letter[-..]="D";
    letter[.]="E";
    letter[..-.]="F";
    letter[--.]="G";
    letter[....]="H";
    letter[..]="I";
    letter[.---]="J";
    letter[-.-]="K";
    letter[.-..]="L";
    letter[--]="M";
    letter[-.]="N";
    letter[---]="O";
    letter[.--.]="P";
    letter[--.-]="Q";
    letter[.-.]="R";
    letter[...]="S";
    letter[-]="T";
    letter[..-]="U";
    letter[...-]="V";
    letter[.--]="W";
    letter[-..-]="X";
    letter[-.--]="Y";
    letter[--..]="Z";
    letter[-----]="0";
    letter[.----]="1";
    letter[..---]="2";
    letter[...--]="3";
    letter[....-]="4";
    letter[.....]="5";
    letter[-....]="6";
    letter[--...]="7";
    letter[---..]="8";
    letter[----.]="9";

    for (( i = 0; i < ${#morse}; i=i + 1 )); do
        echo "${letter:i}"
    done
}

if [[ $1 = "-e" ]]; then
    while IFS= read -r line || [[ -n "$line" ]] ; do
        echo "The text is : $line"
        echo "Morse code is:"
    done < "$2"
    encode "$@"
elif [[ $1 = "-d" ]]; then
    while IFS= read -r line1 || [[ -n "$line1" ]] ; do
        echo "The Morse code is : $line1"
        echo "The translation is:"
    done < "$2"
    decode "$@"
else
    echo "Please try again using -e or -d"
fi


Solution 1:[1]

That may be a late answer, but I think this approach would be simpler:

you will need 3 files :

  • The software
  • The dictionnary
  • The morse to translate (could be piped, or a param)

The logic approach would be to match each morse 'word' (letter) to the dictionary and outputting the result.

A simple dictionary could be like this:

A [.-]
B [-...]
C [-.-.]
D [-..]
E [.]
...

Your input should contain the text to translate on one line: e.g.

.... . .-.. .-.. --- ....... .-- --- .-. .-.. -..

and the software could look like this:

#!/bin/bash
# Get the string from the file (could be a parameter)
str=$(cat test.morse)

# Iterate over every 'word' in the string (split the spaces)
for code in $str; do
        # Find the corresponding line in the dictionnary (dict.csv)
        letter=$(grep -F "[${code}]" dict.csv  | head -c1)
        # Print the letter without caret return
        echo -n $letter
        # if the letter is empty, print a space
        if [[ "$letter" == '' ]]; then
                echo -n ' '
        fi
done
# Print a carret return at the end
echo ""

Solution 2:[2]

I adapted from your code and now this works:

#!/bin/bash

encode_char(){
    declare -A morse
    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]="--..";
    morse[1]=".----";
    morse[2]="..---";
    morse[3]="...--";
    morse[4]="....-";
    morse[5]=".....";
    morse[6]="-....";
    morse[7]="--...";
    morse[8]="---..";
    morse[9]="----.";
    morse[0]="-----";
    morse[ ]="/"

    morse[space]="/"
    morse[return]="\n"

    echo -ne "${morse[${1}]:-/} "  # return "/" if not in dictionary
}

decode_char(){
    declare -A letter
    letter[.-]="A";
    letter[-...]="B";
    letter[-.-.]="C";
    letter[-..]="D";
    letter[.]="E";
    letter[..-.]="F";
    letter[--.]="G";
    letter[....]="H";
    letter[..]="I";
    letter[.---]="J";
    letter[-.-]="K";
    letter[.-..]="L";
    letter[--]="M";
    letter[-.]="N";
    letter[---]="O";
    letter[.--.]="P";
    letter[--.-]="Q";
    letter[.-.]="R";
    letter[...]="S";
    letter[-]="T";
    letter[..-]="U";
    letter[...-]="V";
    letter[.--]="W";
    letter[-..-]="X";
    letter[-.--]="Y";
    letter[--..]="Z";
    letter[-----]="0";
    letter[.----]="1";
    letter[..---]="2";
    letter[...--]="3";
    letter[....-]="4";
    letter[.....]="5";
    letter[-....]="6";
    letter[--...]="7";
    letter[---..]="8";
    letter[----.]="9";
    letter[/]=" ";

    letter[space]=" ";

    echo -ne "${letter[${1}]}"
}




if [[ $1 = "-e" ]]; then
    while IFS= read -r line || [[ -n "$line" ]] ; do
        #echo "The text is : $line"
        #echo "Morse code is:"
        for ch in $(echo "${line^^}" | grep -o . | sed 's/ /space/g') ; do  # convert line to uppercase, then convert to character array, then replace " " with something else, since for loop won't recongnize " " as an item
            encode_char "$ch"
        done
        echo
    done < "$2"
elif [[ $1 = "-d" ]]; then
    while IFS= read -r line1 || [[ -n "$line1" ]] ; do
        #echo "The Morse code is : $line1"
        #echo "The translation is:"
        for ch in $(echo "${line1}" | sed 's./.\nspace\n.g') ; do  # convert "/" into a separate non-empty line
            decode_char "$ch"
        done
        echo " "
    done < "$2"
else
    echo "Please try again using -e or -d"
fi

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vinalti
Solution 2