English 中文(简体)
• 如何创造大量有活力的阵列,有动态名称。
原标题:How to create a dynamic number of arrays with dynamic names in bash?
  • 时间:2024-04-29 01:06:06
  •  标签:
  • arrays
  • bash

我正在使用Bash版本5.1.16。

我正试图写出一个能够创造大量阵列的书,每个书都有一个能动的名称,从一个已申报的阵列中保留一些内容。 我试图将一个阵列分成多个阵列,拥有同样数目的内容。

dec_array = (abc10 def2 ghi333 jkl mno51 pqr_6) # this array will always have some multiple of three elements but variable in the number of elements
num_elem=3
num_arr=0 # will eventually incr and loop once this works
for ((i=0; i < ${#dec_array[@]}; i+=num_elem)); do
    part=( "${dec_array[@]:i:num_elem}" )
    echo "Elements in first example: ${part[*]}"
    dyn_array_${num_arr}=( "${dec_array[@]:i:num_elem}" )
    echo "Elements in second example: $dyn_array_${num_arr}[*]}"
done

产出:

Elements in first example: abc10 def2 ghi333
Elements in first example: jkl mno51 pqr_6

syntax error near unexpected token `"${dec_array[@]:i:num_elem}" 
`    dyn_array_${num_arr}=( "${dec_array[@]:i:num_elem}" ) 

我需要能够将诽谤行为分成多个Dyn_array_n,例如,

dyn_array_0: abc10 def2 ghi333
dyn_array_1: jkl mno51 pqr_6

建立一个具有动态名称的阵列的有活力阵列是正确的yn吗?

问题回答

编码 我们可以使用名称,动态地在飞行中产生变量(包括阵列)。

这样做有几种方法;由于这一回答,我的数字是前线上新阵列的数量,然后是当我们推翻了新阵列时。

#############
# +1 element (stu_XYZ) to demonstrate when element count != multiple of 3:

dec_array=(abc10 def2 ghi333 jkl mno51 pqr_6 stu_XYZ) 

num_elements="${#dec_array[@]}"
elements_per_array=3

#############
# we want ceiling(num_elements / elements_per_array):

num_arrays="$(( (num_elements + elements_per_array - 1) / elements_per_array ))"

index=0

for ((i=1; i<=num_arrays; i++))
do
    declare -n _arr="dyn_array_$i"                       # nameref
    _arr=()                                              # initialize the new array

    for ((j=1; j<=elements_per_array; j++))
    do
        (( index >= num_elements)) && break 2            # once we run out of elements we break out of both for loops
        _arr+=( "${dec_array[index]}" )                  # append current element to _arr[]
        (( index++ ))
    done
done

核实结果,并表明如何在你后面的文字中查阅阵列:

for ((i=1; i<=num_arrays; i++))
do
    declare -n _arr="dyn_array_$i"
    echo "############### ${!_arr}"
    typeset -p "${!_arr}"                                # verify results

    for ((j=0; j<${#_arr[@]}; j++))                      # loop through each new array
    do
         echo "dyn_array_$i[$j] = ${_arr[j]}"            # "${_arr[j]}" ==> individual element reference
    done
done

<>0>NOTE: 2nd echo <>/code> isprint 4 strings:dyn_array_$i[+$j + = + <>>>>>>>>>>>_arr[j]}

这产生了:

############### dyn_array_1
declare -a dyn_array_1=([0]="abc10" [1]="def2" [2]="ghi333")
dyn_array_1[0] = abc10
dyn_array_1[1] = def2
dyn_array_1[2] = ghi333
############### dyn_array_2
declare -a dyn_array_2=([0]="jkl" [1]="mno51" [2]="pqr_6")
dyn_array_2[0] = jkl
dyn_array_2[1] = mno51
dyn_array_2[2] = pqr_6
############### dyn_array_3
declare -a dyn_array_3=([0]="stu_XYZ")
dyn_array_3[0] = stu_XYZ




相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签