store the echo'ed values into an array in bash script
by linxbee from LinuxQuestions.org on (#5KSS9)
Hi,
I am trying to put some content (only numbers) into a variable using cat command and then using an echo statement trying to put into an array.
But, looks like all the values are stored in 0th index only, instead of storing each element in a different index.
Below is my code and am running it using following command:
Quote:
#My content.txt has following data
Quote:
#shell script for extracting numbers into an array
Code:#!/bin/sh
declare -a list
out=$(cat $1)
list=$(echo "$out" | cut -f1 -d:)
for (( i=0; i<${#list[@]}; i++ )); do
echo ${list[i]};
done
echo $list[0]#I am getting the output as following
Quote:
How to properly store echo'ed values into an Array?
I am trying to put some content (only numbers) into a variable using cat command and then using an echo statement trying to put into an array.
But, looks like all the values are stored in 0th index only, instead of storing each element in a different index.
Below is my code and am running it using following command:
Quote:
bash array_exer.sh content.txt |
Quote:
[JUMBO]$ cat content.txt 9:d=1 hl=4 l= 311 cons: SEQUENCE 449:d=1 hl=4 l= 539 cons: SEQUENCE 1362:d=1 hl=4 l= 411 cons: SEQUENCE 1975:d=1 hl=4 l= 639 cons: SEQUENCE [JUMBO]$ |
Code:#!/bin/sh
declare -a list
out=$(cat $1)
list=$(echo "$out" | cut -f1 -d:)
for (( i=0; i<${#list[@]}; i++ )); do
echo ${list[i]};
done
echo $list[0]#I am getting the output as following
Quote:
[JUMBO]$ bash array_exer.sh content.txt 9 449 1362 1975 9 449 1362 1975[0] [JUMBO]$ |