ok now im working on another code. i have to add two arrays together to form one array.
all that i am getting for the output is 2. here is the code...i know i am missing a few things but i just do not know what exactly. i # some of my statements out to get some kind of result out. any advice is appreciated thanks

#!/bin/bash

a=(1 3 5 7 8)

b=(2 3 5 8 9 2)

c=( )

#size1=${#a[*]}

#size2=${#b[*]}

#echo "the size of 1: $size1"

#echo "the size of 2: $size2"

cnt=0

cnt1=0

cnt2=0

echo;

while [ $cnt -le ${#a[*]} -a $cnt -le ${#b[*]} ]
do
#       echo ${a[cnt1]}${b[cnt2]}
        c=$((${a[cnt1]}+${b[cnt2]}))
        cnt=$((cnt+1))
        cnt1=$((cnt1+1))
        cnt2=$((cnt2+1))
done

echo "now the array is: ${c[*]}"

echo;

exit

Recommended Answers

All 3 Replies

Hey There,

check out this page for just about everythign you might need to know about bash arrays (at least, a lot of the basics ;) - This part addresses your issue, but the whole page is great!

http://tldp.org/LDP/abs/html/arrays.html

# Combine (append) two arrays into a third array.
echo
echo 'Conditions: Unquoted, default IFS, All-Elements-Of operator'
echo '- Undefined elements not present, subscripts not maintained. -'
# # The undefined elements do not exist; they are not being dropped.

dest=( ${array1[@]} ${array2[@]} )
# dest=${array1[@]}${array2[@]} # Strange results, possibly a bug.

# Now, list the result.
echo
echo '- - Testing Array Append - -'
cnt=${#dest[@]}

echo "Number of elements: $cnt"
for (( i = 0 ; i < cnt ; i++ ))
do
echo "Element [$i]: ${dest[$i]}"
done

Best wishes,

Mike

ok thank you so much. that really did help

Cool :)

Glad to be of some help :)

, Mike

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.