Thursday 4 October 2012

arrays in shell script

I needed to make changes in a file that is downloaded to the system,
the changes include inserting the database name, database user_name,
etc. Firstly I tried so many methods but they were insufficient and
did not solve the purpose correctly, so I tried changes to it and was
able to do so. I could make changes to the settings.py file( the file
i wanted to make changes to) using "sed" command(sed=stream editor).
There was one big problem, I was using the same command multiple
times, so I used the for loop and put the different elements in
arrays.
Harman's mail on using arrays in shell script was very helpful, I
faced very less problem thanks to it. Secondly, I restricted the use
of sed to particular lines(rows in file) specifying the line in a
array .
Now what I have just made(just the main part):


###########################################################
array1=("db_name" "db_user" "db_password" "user_name" "email_add")
array2=("15" "16" "17" "86" "110")

len=${#array[*]}

file=Automation/settings.py

i=0
while [ $i -lt $len ]; do
        read -p "${array[$i]}" ${array1[$i]}
        #echo ${!array1[$i]}
        sed -i "${array2[$i]} s/${array1[$i]}/${!array1[$i]}
/" $file
        let i++
done
###########################################################

This code works fine, but I need to improve it.