2

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
LMC
  • 10,453
  • 2
  • 27
  • 52
Philip
  • 71
  • 1
  • 1
  • 6
  • Start with shellcheck.net. You've also got a problem with using `readarray` in a pipe, although that line is somewhat irrelevant since you would be overriding whatever is in the array in the very next line that correctly uses process substitution to populate the array. – chepner Mar 27 '18 at 22:57
  • (And what's with the unused `curlOptions` when you are using `wget` anyway?) – chepner Mar 27 '18 at 22:57
  • It comes up later in the script, this is only half of it. I just can't even get it to count. – Philip Mar 27 '18 at 23:01
  • You've got a few issues here, note @chepner's point about the first time you do `readarray`. But, take this one step at a time. After each operation you do on `myWebsitesarray`, take a look at its contents and make sure they match what you expect. For instance, try `echo "${#myWebsitesarray[@]:1}"` after your second `readarray`. – TTT Mar 28 '18 at 00:22
  • I cleaned it up, it's working correctly. I appreciate the tips! – Philip Mar 28 '18 at 01:31

1 Answers1

1

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.

LMC
  • 10,453
  • 2
  • 27
  • 52