Shell Script - Alphabet in code

Hi e
Hi everyone, I can't make this script work,

#! /bin/bash declare -A crypt=(     ['@_banana']="A"     ['99_melon']="a"     ['AZ_GRAPE']="B"     ['!!_stars']="b"     ['81_xxxxxx']="C"     ['computer']="c"     ['111_space']=' '     ['life_9900']='!' )  encode () {     local word=$1     for ((i=0; i<${#word}; ++i)) ; do         local char=${word:$i:1}         printf %s' ' ${crypt[$char]}     done     printf '\n' }  declare -A decrypt for char in "${!crypt[@]}" ; do     key=${crypt[$char]}     decrypt[$key]=$char done  decode () {     local word=$1     while [[ $word ]] ; do         local code         for code in "${!decrypt[@]}"; do             if [[ $word == "$code"* ]] ; then                 printf %s "${decrypt[$code]}"                 word=${word#"$code"}             fi         done     done     printf '\n' }  encode '@_banana!!_starscomputerlife_9900'
Output: 

I can't make the output work

should work like this

encode '@_banana!!_starscomputerlife_9900'
Output: Abc! 

wanted it to work to decode the output

Welcome on board!

Thanks in applying our rules, not often we see newcomers doing that from the beginning...
Now, for what reason is your code a one liner? Do you think people will loose time going through such a line?
Making your code easy to read will have far more success...

the code is not linear I pasted it here in the forum and it came out like this

--- Post updated at 04:29 PM ---

#! /bin/bash declare -A crypt=(     ['@_banana']="A"     ['99_melon']="a"     ['AZ_GRAPE']="B"     ['!!_stars']="b"     ['81_xxxxxx']="C"     ['computer']="c"     ['111_space']=' '     ['life_9900']='!' )  encode () {     local word=$1     for ((i=0; i<${#word}; ++i)) ; do         local char=${word:$i:1}         printf %s' ' ${crypt[$char]}     done     printf '\n' }  declare -A decrypt for char in "${!crypt[@]}" ; do     key=${crypt[$char]}     decrypt[$key]=$char done  decode () {     local word=$1     while [[ $word ]] ; do         local code         for code in "${!decrypt[@]}"; do             if [[ $word == "$code"* ]] ; then                 printf %s "${decrypt[$code]}"                 word=${word#"$code"}             fi         done     done     printf '\n' }  encode '@_banana!!_starscomputerlife_9900'
#! /bin/bash 
declare -A crypt=(     ['@_banana']="A"     ['99_melon']="a"     ['AZ_GRAPE']="B"     ['!!_stars']="b"     ['81_xxxxxx']="C"     ['computer']="c"     ['111_space']=' '     ['life_9900']='!' )  

encode () 
{     
local word=$1     
for ((i=0; i<${#word}; ++i)) ; 
do
         local char=${word:$i:1}
         printf %s' ' ${crypt[$char]}
done
printf '\n' 
}  

declare -A decrypt 
for char in "${!crypt[@]}" 
do     
  key=${crypt[$char]}     
  decrypt[$key]=$char
done
 
decode () 
{     
local word=$1     
while [[ $word ]] ; 
do
         local code
         for code in "${!decrypt[@]}"; 
         do 
               if [[ $word == "$code"* ]] ; 
               then
                    printf %s "${decrypt[$code]}"
                    word=${word#"$code"}
               fi
         done
done
     printf '\n' 
} 
 
encode '@_banana!!_starscomputerlife_9900'

Your code reformatted as far as I can understand, I let you correct from there, and perhaps people now may get involved...

2 Likes

Hi
maybe so?

...
encode () {
        local word=$1
        for word in $1; do
                echo -n "${crypt[$word]} "
        done
        echo
}
...
encode '@_banana !!_stars computer life_9900'

--- Post updated 12-12-19 at 00:23 ---

or it's better to get by with multiple parameters because in the "decode" procedure
it is necessary to iterate with a space and it is better to adhere to the same style of calling functions

encode () {
        for word in $@; do
                echo -n "${crypt[$word]} "
        done
        echo
}
encode @_banana !!_stars computer 111_space life_9900
decode () {
        while [ "$1" ]; do
                echo "$1"
               ...
                shift
        done
}
decode A a b c ' ' !
2 Likes

Your encode should pretty much be the same function as your decode, just use crypt array instead of decrypt:

encode ()
{
local word=$1
while [[ $word ]] ;
do
         local code
         for code in "${!crypt[@]}";
         do
               if [[ $word == "$code"* ]] ;
               then
                    printf %s "${crypt[$code]}"
                    word=${word#"$code"}
               fi
         done
done
printf '\n'
}
1 Like