K
K
ksvdon2014-03-19 15:40:15
bash
ksvdon, 2014-03-19 15:40:15

BASH - how to write the values ​​of a variable (in a loop) to different elements of an array?

#!/bin/bash
cat /user/test | while read line
do
case $line in
number?of?seconds* )
MASS=(`echo $line | grep -o '[0-9]\.[0-9][0-9][0-9]' `)
echo ${MASS[0]}
esac
done
In a loop, I extract the necessary lines from the text (according to the pattern) and I need to take numbers from these lines for further processing. I decided to add numbers to the array, so that later I can access the elements of the array.
Finds lines. Outputs numbers. BUT everything writes to the first element of the array. As if I'm driving into one variable. How to make bash write each value to a new array element?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2014-03-19
@ksvdon

BUT everything writes to the first element of the array

Well, yes, you recreate the array in each iteration.
Can do this?:
#!/bin/bash
declare -a MASS
cat /user/test | while read line
do
case $line in
number?of?seconds* )
    MASS[${#MASS[@]}]=`echo $line | grep -o '[0-9]\.[0-9][0-9][0-9]'`
    echo ${MASS[-1]}
    ;;
esac
done

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question