Monday, January 23, 2012

some more array operations

Some help links
arithmetic operations :
http://bash.cyberciti.biz/guide/Perform_arithmetic_operations

for arrays:
http://www.arachnoid.com/linux/shell_programming.html
http://tldp.org/LDP/abs/html/arrays.html

master_slaves.txt is a file having lines separrated by ':' that needs to converted to array and then processed

>>cat master_slaves.txt
master_config:root:venableroot:3306:ml1
slave_config:ml2:ml3

master= array of values from line 1
slave = array of values from line 2


---------------------------------- script -----------------------------------
!/bin/bash -ex
p_r="test"
filename="$p_r/master_slave.txt"
master=()
slaves=()

while read line
do
table=$line

splits=(`echo "$table" | tr ':' ' '`)

if [ "${splits[0]}" = "master_config" ]
then
master=(${splits[*]})
elif [ "${splits[0]}" = "slave_config" ]
then
slave=(${splits[*]})
else
continue
fi

if [ "$master" ] && [ "$slave" ]
then

lenm="${#master[*]}"
echo "lenm=$lenm"
lens="${#slave[*]}"
echo "lens=$lens"

## append master to slave
slave[$lens]=${master[4]}
lens=$(( lens + 1 ))

i=0;
while [ $i -lt $lenm ]
do


echo "master:${master[$i]}"
i=$(( i + 1 ))
done

i=0;
while [ $i -lt $lens ]
do

echo "master:${slave[$i]}"
i=$(( i + 1 ))
echo "i=$i"

done

else
echo "master and slave config improper. check $filename"
fi

done < ${filename}
echo "over"

No comments:

Post a Comment