Register
It is currently Mon Jul 28, 2014 6:34 pm

Running Functions in Bash While Converting Letters to Number


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Mon May 19, 2014 8:30 pm   

Joined: Mon May 19, 2014 8:11 pm
Posts: 3
Hello all, I've recently gotten into Bash script-writing. I'm trying to write a cryptographic script that will convert letters into numbers, switch the numbers around, and convert the modified numbers into letters. My problem is that when I run the script I get the error "./C01_help.sh: line 9: =1: command not found" For some reason, I can't upload "C01_input.txt", but all it contained are the three sentences: Hello World! How are you doing? I am doing fine. (EOF) What follows is my code:


Code:
#!/bin/bash
# To compile the program type (in the terminal): chmod +x C01_help.sh
# To run the program type (in the terminal):
# ./C01_help.sh C01_input.txt

function letters2numbers() {
   if [[ "$1" == "a" ]]
   then
      ${number_array[$2]}=1
   elif [ "$1" == "b" ]
   then
      echo "2"
   else
      echo "3"
   fi
}

string2letters() {
array=$1
array_length=${#array[@]}

for (( index=0; index <= $array_length - 1; index++ ))
do
   word_length=${#array[$index]}
   word_array[0]="a"
   for (( index2=0; index2 <= $word_length - 1; index2++ ))
   do
      word_array[$index2]=${array[$index]:$index2:1}
      letters2numbers "${word_array[$index2]}" "$index2"
   done
   unset word_array
done
}

# "Main Function"

while read line
do
   IFS=' ' read -a array <<< $line
   temp_string=""
   for (( index=0; index <= ${#array}; index++ ))
   do
      temp_string="${array[$index]}"
      string2letters "$temp_string"
   done
done < $1


Any help would be greatly appreciated.


Top
 Profile  
 PostPosted: Tue May 20, 2014 5:56 am   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 532
hi,

Code:
number_array[$2]=1
that's assigning, not expanding.


Top
 Profile  
 PostPosted: Tue May 20, 2014 11:51 am   

Joined: Mon May 19, 2014 8:11 pm
Posts: 3
I tried that and all it returned was several "3". There should be two "1" in the output as well.


Top
 Profile  
 PostPosted: Wed May 21, 2014 4:22 pm   

Joined: Mon May 19, 2014 8:11 pm
Posts: 3
Nevermind, I figured it out.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP