Answer the question
In order to leave comments, you need to log in
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
BUT everything writes to the first element of the array
#!/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 questionAsk a Question
731 491 924 answers to any question