加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

数组 – Bash:数组是否可以保存另一个数组的名称?

发布时间:2020-12-15 21:08:03 所属栏目:安全 来源:网络整理
导读:我正在编写一个程序并试图分解存储在数组中的数据,以使其运行得更快. 我试图这样做: data_to_analyze=(1 2 3 4 5 6 7 8 9 10) #original array size dataSize=(${#data_to_analyze[@]}) #half of that size let samSmall="$dataSize/2" #the other half let
我正在编写一个程序并试图分解存储在数组中的数据,以使其运行得更快.
我试图这样做:

data_to_analyze=(1 2 3 4 5 6 7 8 9 10)
    #original array size
    dataSize=(${#data_to_analyze[@]})

    #half of that size
    let samSmall="$dataSize/2"

    #the other half
    let samSmall2=("$dataSize - $samSmall -1")

    #the first half
    smallArray=("${data_to_analyze[@]:0:$samSmall}")

    #the rest
    smallArray2=("${data_to_analyze[@]:$samSmall:$samSmall2}")

    #an array of names(which correspond to arrays)
    combArray=(smallArray smallArray2)
    sizeComb=(${#combArray[@]})

    #for the length of the new array
    for ((i=0; i<= $sizeComb ; i++)); do
        #through first set of data and then loop back around for the second arrays data? 
        for sample_name in ${combArray[i]}; do
            command
            wait
            command
            wait
        done

我的想法是,它首先只给出for循环的第一个数据数组.第一个数组完成后,应该再次使用第二个数组.

这让我有两个问题. combArray真的通过了两个较小的数组吗?还有更好的方法吗?

解决方法

#!/bin/bash
data_to_analyze=(1 2 3 4 5 6 7 8 9 10)
dataSize=${#data_to_analyze[@]}
((samSmall=dataSize/2,samSmall2=dataSize-samSmall))
smallArray=("${data_to_analyze[@]:0:$samSmall}")
smallArray2=("${data_to_analyze[@]:$samSmall:$samSmall2}")
combArray=(smallArray smallArray2)
sizeComb=${#combArray[@]}
for ((i=0;i<$sizeComb;i++));do
    eval 'a=("${'${combArray[i]}'[@]}")'
    for sample_name in "${a[@]}";do
        ...
    done 
done

编辑:删除${combArray [i]}附近的双引号并替换< = by<为...

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读