0

How can I store numbers from two loops into one array and take average of those numbers?

#!/bin/bash
START_se1=55761
END_se1=55770
START_set2=55900
END_set2=55917
#set1
for ID in {$START_set1..$END_set1}
do
  myarr1=($(echo ${ID}))
done

#set2
for ID in {$START_set2..$END_set2}
do
  myarr2=($(echo ${ID}))
done

app=( "${myarr1[@]}" "${myarr2[@]}" )
echo $app

This code gives only last ID in myarr1 which is 55770

Thanks

hash
  • 103
  • 10
  • Probably related: [How do I iterate over a range of numbers defined by variables in bash?](http://stackoverflow.com/q/169511/1983854). Not sure if it is the case – fedorqui Jan 28 '15 at 11:58
  • you do not need to run command execution `$(...)`. just assign the id. `{$START_set1..$END_set1}` really works? if so which shell hides behind bash? - ok - learned something :) – Marc Bredt Jan 28 '15 at 12:29

1 Answers1

0

initialize your array before the loop myarr1=() and fill it using myarr1[${#myarr1[@]}]=${ID}. print out after you filled it with echo ${myarr1[@]}.hope this helps.

UPDATE: you got some typos, list and access faults. consider this snippet.

#!/bin/bash
START_set1=55761 # typo
END_set1=55770 # typo
START_set2=55900
END_set2=55917

#set1
myarr1=()
for ID in $(seq $START_set1 1 $END_set1); do # typo, range iteration
  myarr1[${#myarr1[@]}]=${ID} # fill set1
done
echo ${myarr1[@]}
echo ---

#set2
myarr2=()
for ID in $(seq $START_set2 1 $END_set2); do # range iteration
  myarr2[${#myarr2[@]}]=${ID} # fill set2
done
echo ${myarr2[@]}
echo ---

app=( "${myarr1[@]}" "${myarr2[@]}" )
echo ${app[@]} # access all elements, like above
Marc Bredt
  • 905
  • 5
  • 13