fork(1) download
  1. #!/bin/bash
  2. declare -A MORSE=( [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]='--..' [1]='.----' [2]='..---' [3]='...--' [4]='....-' [5]='.....' [6]='-....' [7]='--...' [8]='---..' [9]='----.' [0]='-----' [',']='--..--' ['.']='.-.-.-' [';']='-.-.-.' [':']='---...' ['?']='..--..' ['!']='-.-.--' ['/']='-..-.' ['-']='-....-' ['+']='.-.-.' ['(']='-.--.' [')']='-.--.-' ['_']='..--.-' ['"']='.-..-.' ["'"]='.----.' ['$']='...-..-' ['@']='.--.-.' ['&']='.-...' [' ']=' ' )
  3.  
  4. function encode {
  5. res=''
  6. s="$1"
  7. for (( i=0; i<${#s}; i++ )); do
  8. letter="${s:$i:1}"
  9. if [[ "$letter" == ' ' ]]; then
  10. res="${res} "
  11. else
  12. res="${res}${MORSE[${letter^^}]} ";
  13. fi
  14. done
  15. printf "%s" "$res"
  16. }
  17.  
  18. echo "$(encode "THIS IS FINE")"
  19.  
  20. declare -A MORSEDEC=( ['-.--.-']=')' ['..--..']='?' ['--..--']=', ' ['-....-']='-' ['.-.-.-']='.' ['...--']='3' ['-.--.']='(' ['---..']='8' ['-..-.']='/' ['....-']='4' ['-....']='6' ['----.']='9' ['.----']='1' ['..---']='2' ['.....']='5' ['--...']='7' ['-----']='0' ['-...']='B' ['-..-']='X' ['-.-.']='C' ['--..']='Z' ['--.-']='Q' ['.-..']='L' ['-.--']='Y' ['..-.']='F' ['.--.']='P' ['.---']='J' ['...-']='V' ['....']='H' ['-..']='D' ['---']='O' ['..-']='U' ['...']='S' ['.--']='W' ['-.-']='K' ['.-.']='R' ['--.']='G' ['-.']='N' ['..']='I' ['--']='M' ['.-']='A' [' ']=' ' ['.']='E' ['-']='T' )
  21.  
  22. function decode {
  23. res=''
  24. tmp="$(sed 's/ \{2,\}/ | /g' <<< "$1")";
  25. for word in $tmp; do
  26. if [[ "$word" == '|' ]]; then
  27. res="${res}${MORSEDEC[' ']}";
  28. else
  29. res="${res}${MORSEDEC[$word]}";
  30. fi
  31. done
  32. printf "%s" "$res"
  33. }
  34. echo "$(decode "- .... .. ... .. ... ..-. .. -. .")"
Success #stdin #stdout 0s 4292KB
stdin
Standard input is empty
stdout
- .... .. ...   .. ...   ..-. .. -. . 
THIS IS FINE