Posted: Sun Oct 24, 2010 7:56 am
I guess this one earns me the award for the most bloated approach:
Code: Select all
#!/bin/bash
file=val.txt
i=0
while IFS= read -r -n1 val_char
do
x_array[i]=$val_char
let i++
done < "$file"
j=0
array_count=0
while [ ${x_array[j]} != "" ]
do
if [ ${x_array[j]} != 'x' ]
then
final_array[$array_count]=${x_array[j]}
let array_count++
let j++
else
num1=`expr $array_count - 2`
num2=`expr $array_count - 1`
array_count_plus=`expr $array_count + 1`
final_array[$array_count]=${final_array[$num1]}
final_array[$array_count_plus]=${final_array[$num2]}
let j++
array_count=`expr $array_count + 2`
fi
done
k=0
l=`expr $i - 1`
index=`expr ${#final_array[@]} - 1`
for k in $(seq 0 $index );
do
result=`expr $result + ${final_array[$k]}`
done
echo "Result: " $result