bash中数组的动态变量名

时间:2017-06-05 23:37:36

标签: arrays bash

在我的脚本中,我循环遍历第一个数组的所有元素"循环"。对于"循环中的每个元素"我想创建一个数组,其名称包含来自" loop"的当前元素的文本。然后我想循环创建这些新的数组并打印其中包含的所有元素。

This post似乎有一个很好的变量解决方案,但我不知道如何使其适应数组。

脚本

#!/bin/bash
loop=(
first
second
third
)

for word in "${loop[@]}"    
do
        declare "${word}_holder=( hello world )"
        var="${word}_holder"
        echo "$var"
        for i in "${!var}[@]"
        do
                echo "$i"
        done
done

当前输出

first_holder
( hello world )[@]
second_holder
( hello world )[@]
third_holder
( hello world )[@]

期望输出

first_holder
hello
world
second_holder
hello
world
third_holder
hello
world

2 个答案:

答案 0 :(得分:4)

它很漂亮,但它是一个开始:

#include<stdio.h>
#include<math.h>
#include<stdbool.h>
#include<time.h>
#define LIMIT 250000ul

//prime number generator using arrays 

void prime_generator(){

    unsigned long long n; 
    printf("How many primes do you want generated : "); 
    scanf("%llu", &n);

    unsigned long long p[LIMIT];  
    unsigned long long i, j=2; 
    unsigned long long num; 
    bool isPrime; 

    unsigned long long primes_per_line, space_column; 
    printf("How many primes do you want displayed per line? : "); 
    scanf("%llu", &primes_per_line); 
    printf("Enter space : "); 
    scanf("%llu", &space_column); 

    p[0]=2, p[1]=3;//first two primes
    unsigned long long count = 2; 

    clock_t begin = clock();   
    for(num = 5;count < n; num=num+2){//only test for odd number 
        isPrime = true; 
        for(i = 1;i < j && p[i] <= sqrt(num) ;i++){
            if(num%p[i]==0){
                isPrime = false;  
                break;//break out of i loop 
            }
        }
        if(isPrime == true){
            ++count; 
            p[j] = num; //new prime found 
            ++j; 
        }
    }
    clock_t end = clock(); 
    double time_spent = (double) (end-begin)/CLOCKS_PER_SEC; 

    for(i = 0; i < n; i++){
        if(i!=0 && i % primes_per_line == 0) printf("\n"); 
        printf("%*llu", space_column, p[i]); 
    }

    printf("\nCrunching time is : %.12f", time_spent);
}

int main(void){
    prime_generator(); 
    return 0; 
}

答案 1 :(得分:4)

for word in "${loop[@]}"; 
do 
   name="${word}_holder";
   declare -a "$name"; 
   declare -n arr="$name";
   echo $name;
   arr=(hello world);
   arr_ref="$name[@]";
   for w in "${!arr_ref}";
   do 
      echo $w; 
   done; 
done;                             

first_holder                                                                                                                              
hello                                                                                                                                     
world                                                                                                                                     
second_holder                                                                                                                             
hello                                                                                                                                     
world                                                                                                                                     
third_holder                                                                                                                              
hello                                                                                                                                     
world     

当然,如果你不打算引用动态生成的数组(first_holder等),那么就没有必要这样做。