0

i've been trying to get something like this to work for a while now but I keep running into little imperfection that mess up building my array.

#!/bin/bash

There are a total of four lines in this repo file

repofile=~/Home/Documents/repoKali

I type this into the command line

wc -l < $repofile

I get the following output

6

But when I type this

SIZE=$(wc -l < "$repofile")

I get this

6: command not found

I'm trying build an array that is as big as the number of lines in $repofile. I'm not sure why the commands work outside of variable assignment and not when I assign them to SIZE. I mean the output changes! or am I just missing something?

Please Help. I'm trying to do something like this.

readarray -s $SIZE < $repofile

Seanny123
  • 8,776
  • 13
  • 68
  • 124
arcad31a
  • 1
  • 1

1 Answers1

1

You don't need to initialize bash arrays with a size, just put values into the array.

The -s option for readarray is not for "size", it's for "skip":

Options: ...

-s count   Discard the first COUNT lines read

From a bash prompt, type help readarray for all the details.


This error 6: command not found indicates to me that you're putting a space after the = sign: no spaces are allowed for variable assignment

SIZE= $(wc -l < "$repofile")
#....^
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • Thank YOu. I appreciate you getting back to me. I feel stupid for just skimming the man page. I'll refer back and try your suggestion. and let you know how it goes. Thanks again, man. – arcad31a Oct 14 '18 at 00:15
  • yeah that worked. I did readarray repolist < $repofile then echo ${repolist[@]} I got the output I was looking for. Thanks again! – arcad31a Oct 14 '18 at 00:33
  • Remember to always quote the array expansion: `"${repolist[@]}"` -- https://stackoverflow.com/q/12314451/7552 – glenn jackman Oct 14 '18 at 00:35