如何为循环提供两组范围

时间:2015-01-28 11:56:35

标签: bash

如何将两个循环中的数字存储到一个数组中并取这些数字的平均值?

#!/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

此代码仅提供myarr1中的最后一个ID,即55770

由于

1 个答案:

答案 0 :(得分:0)

在循环myarr1=()之前初始化数组并使用myarr1[${#myarr1[@]}]=${ID}填充它。用echo ${myarr1[@]}填写之后打印出来。希望这有帮助。

更新:您遇到了一些拼写错误,列表和访问错误。考虑这个片段。

#!/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