Answer the question
In order to leave comments, you need to log in
How to loop through arrays in bash?
I have some text. Log of some values. I break this text into elements and throw it into an array for easy comparison with readings from other sources. I want to be able to create several arrays in a loop so as not to select by time, but simply drive all the values into different arrays for a certain period and compare with the standard. I tried to stick a variable into the array name (a number that can be increased in a loop and in such a way that another array would be created each time) of the type: MASS[0]=`что-то там`
you simply p=0 MASS$p[0]=`что-то там`
cannot insert a variable. Here is the question. How can you?
I look at how many suitable strings will go into arrays; in the while []; do loop I want to create as many arrays as the number of lines the script calculates.
Answer the question
In order to leave comments, you need to log in
I can suggest a better option. Don't create multiple arrays, just use arithmetic to calculate the index.
#!/bin/bash
for i in {1..5}
do
for j in {1..5}
do
INDEX=$((i*1000+j))
ARRAY[INDEX]=$j
done
done
echo Full array:
echo ${ARRAY[*]}
echo First line:
for j in {1..5}
do
INDEX=$((1000+j))
echo ${ARRAY[INDEX]}
done
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question