Shell Script - Alphabet in code


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Shell Script - Alphabet in code
# 1  
Old 12-11-2019
Shell Script - Alphabet in code

Hi e
Hi everyone, I can't make this script work,
Code:
#! /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'


Code:
Output:


I can't make the output work



should work like this


Code:
encode '@_banana!!_starscomputerlife_9900'



Code:
Output: Abc!




wanted it to work to decode the output
# 2  
Old 12-11-2019
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...
# 3  
Old 12-11-2019
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'

Last edited by RavinderSingh13; 12-11-2019 at 09:38 PM..
# 4  
Old 12-11-2019
Code:
#! /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...

Last edited by Scrutinizer; 12-11-2019 at 03:19 PM.. Reason: Some corrections..
These 2 Users Gave Thanks to vbe For This Post:
# 5  
Old 12-11-2019
Hi
maybe so?
Code:
...
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
Code:
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 ' ' !

These 2 Users Gave Thanks to nezabudka For This Post:
# 6  
Old 12-12-2019
Your encode should pretty much be the same function as your decode, just use crypt array instead of decrypt:

Code:
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'
}

This User Gave Thanks to Chubler_XL For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional for every letter in alphabet

I wanted to know if there was a more efficient to do this. I was to setup a conditional for every letter of the alphabet, like so (I am parsing an array): for i in "${arr}"; do if ]; then echo "$i starts with A" else echo "$i does not start with A" fi done I want to do this A-Z, is there... (6 Replies)
Discussion started by: sudo
6 Replies

2. Shell Programming and Scripting

How to capture the exit code of a shell script in a perl script.?

hi, i want to pop up an alert box using perl script. my requirement is. i am using a html page which calls a perl script. this perl script calls a shell script.. after the shell script ends its execution, i am using exit 0 to terminate the shell script successfully and exit 1 to terminate the... (3 Replies)
Discussion started by: Little
3 Replies

3. Shell Programming and Scripting

Help with Shell script code

Hello all, I am in a middle of an assignment and i would appreciate any help. How can i write a bash shell script code that checks if all elements in an array are the same numbers. I mean -->array = ( 0,0,0,0,0 ) ( e.g., if then return "OK' fi ) Thank you in advance, (9 Replies)
Discussion started by: Geekie
9 Replies

4. Shell Programming and Scripting

Help with sort alphabet on specific column

Input file: POL B7U6K8 Avian_reticuloendotheliosis_virus POLB B7Z1W5 Homo_sapiens POLB H9G5Y0 Anolis_carolinensis POLD1 Q642R8 Xenopus_laevis POLD2 H0YZC7 Taeniopygia_guttata POLD3 F1P540 Gallus_gallus POLDIP3 Q5F4B6 Gallus_gallus POLE2 E1C2T8 Gallus_gallus... (3 Replies)
Discussion started by: perl_beginner
3 Replies

5. Shell Programming and Scripting

Recode alphabet into numbers

I have a genotype.bim file where it contains information about SNPs and genotype. As a hypothetical example, let's say genotype.bim snp1 ... A G snp2 ... G T snp3 ... G T snp4 ... G A ... snpN ... C G where first column identifies each SNP and 5th and 6th column has genotype... (3 Replies)
Discussion started by: johnkim0806
3 Replies

6. Shell Programming and Scripting

last character is digit or alphabet!

Hello, I have to find out whether the last character is digit or alphabet. I manage to strip the last character but would need some help if there is one liner available to test the above. set x = WM echo $x | sed 's/.*\(.$\)/\1/' O/P M I would like a one liner code to test whether the... (1 Reply)
Discussion started by: dixits
1 Replies

7. Shell Programming and Scripting

Alphabet counting

I have a text file in the following format CCCCCGCCCCCCCCCCcCCCCCCCCCCCCCCC AAAATAAAAAAAAAAAaAAAAAAAAAAAAAAA TGTTTTTTTTTTTTGGtTTTTTTTTTTTTTTT TTTT-TTTTTTTTTCTtTTTTTTTTTTTTTTT Each row/line will have 32 letters and each line will only have multiple occurrences of 2 letters out of a pool... (1 Reply)
Discussion started by: Lucky Ali
1 Replies

8. Shell Programming and Scripting

shell script to find the second occurence of the alphabet in a string

this is my assignment question. i'm supposed to submit it tommorow. can somebody please help me with it? Do not post homework questions in the main forums. Please post in the homework forum using the correct template. (0 Replies)
Discussion started by: vijjy
0 Replies

9. Shell Programming and Scripting

To check if the first character is a alphabet or number

Hi, I need to find whether the first character in a line is a alphabet or a number. If its a number i should sort it numerically. If its a alphabet i should sort it based on the ASCII value.And if it is something other than alphabet or number then sort it based on ASCII value. The code i used... (2 Replies)
Discussion started by: ragavhere
2 Replies

10. Shell Programming and Scripting

What can i do to check that the input is all alphabet.. ?

What can i do to check that the input is all alphabet.. ? (4 Replies)
Discussion started by: XXXXXXXXXX
4 Replies
Login or Register to Ask a Question