Philip
Philip

Reputation: 71

Bash - readarray contains only one element

I'm writing this script to count some variables from an input file. I can't figure out why it is not counting the elements in the array (should be 500) but only counts 1.

#initializing variables 
timeout=5
headerFile="lab06.output"
dataFile="fortune500.tsv"
dataURL="http://www.tech.mtu.edu/~toarney/sat3310/lab09/"
dataPath="/home/pjvaglic/Documents/labs/lab06/data/"
curlOptions="--silent --fail --connect-timeout $timeout"

#creating the array
declare -a myWebsitearray #=('cut -d '\t' -f3 "dataPath$dataFile"')

#obtaining the data file
wget $dataURL$dataFile -O $dataPath$dataFile

#getting rid of the crap from dos
sed -e "s/^m//" $dataPath$dataFile | readarray -t $myWebsitesarray

readarray -t myWebsitesarray < <(cut -d, -f3 $dataPath$dataFile)

myWebsitesarray=("${#myWebsitesarray[@]:1}")

#printf '%s\n' "${myWebsitesarray2[@]}"

websitesCount=${#myWebsitesarray[*]}

echo $websitesCount

Upvotes: 2

Views: 523

Answers (1)

LMC
LMC

Reputation: 12662

You are overwriting your array with the count of elements in this line

myWebsitesarray=("${#myWebsitesarray[@]:1}")

Remove the hash sign

myWebsitesarray=("${myWebsitesarray[@]:1}")

Also, @chepner suggestions are good to follow.

Upvotes: 1

Related Questions