[K12OSN] OT: Bash help

Les Mikesell les at futuresource.com
Mon Nov 19 18:33:41 UTC 2007


Peter Hartmann wrote:
> I'm trying to set up a large number of arrays with a for do loop.
> It's not working, so to troubleshoot I'm simplifying it and only
> seeing if can set up an array variable with a variable.
> 
> LIST=ffirst
> 
> #Declare array
> declare -a ffirst
> #Open file for reading to array
> exec 10<ffirst   #  ffirst is also my filename
> let count=0
> 
> while read LINE <&10; do
> 
>    $LIST[$count]=$LINE
>     ((count++))
> done
> exec 10>&-
> 
> I's choking like this:
> 
> ./create-csv3: line 62: ffirst[0]=Carmine: command not found
> ./create-csv3: line 62: ffirst[1]=: command not found
> ./create-csv3: line 62: ffirst[2]=: command not found
> ./create-csv3: line 62: ffirst[3]=Leonardo: command not found
> ./create-csv3: line 62: ffirst[4]=William: command not found
> ./create-csv3: line 62: ffirst[5]=: command not found
> 
> 
> Is there a way to do what I want?

First, if you need arrays, you should probably think perl and hashes 
instead of shell and counters, but your problem is that shell variable 
assignments don't want the leading $, so make it
LIST[$count]=$LINE.

Also, you could skip the exec and file descriptor setup by simply 
redirecting the loop at the 'done; line like:
   done < ffirst

-- 
   Les Mikesell
    les at futuresource.com




More information about the K12OSN mailing list