[K12OSN] OT: Bash help

Peter Hartmann ascensiontech at gmail.com
Tue Nov 20 00:57:33 UTC 2007


> 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.

Thanks Les...

I needed eval to make this part work:

 #!/bin/bash
 a=name
eval  $a=b

echo $name

now the result is 'b'

Sadly this doesn't seem to work for array varables.  I guess I'm
looking to make an array of arrays.

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

I'm not sure what the resulting code looks like...  But I'll tell you
that I need to preserve blank lines in my textfiles.  Some of the ways
I tried to assign a texfile to an array removed those blank lines.

Guess I'll be studying perl......


Thanks,
Peter

On Nov 19, 2007 2:16 PM, Peter Hartmann <peter at hartmanncomputer.com> wrote:
> Hey Robert,
> I always forget to paint the bigger picture....I'm making a csv file
> from many textfiles.    This last part works well....so I'm hoping to
> keep it.  Eventually there will be LOTS of fields, not just 2.
>
> LIMIT=400
> for ((a=0; a <= LIMIT ; a++))
> do
>    echo  ${ffirst[$a]}:${flast[$a]} # >> contacts.csv
> done
> echo; echo
>
> Well, I have about 40-50 arrays to set up from files...and I'm lazy.
> ;)  Just trying to automate it. I guess I could always write a script
> to write that portion of the script.
>
> What it boils down to is this:  (this is out of context to illustrate
> my confusion)
>
> #!/bin/bash
> a=name
> $a=b
>
> echo $name
>
> What I want to get from this script is "b"  but instead I get:
> ./vartest: line 3: name=b: command not found
>
> I want the shell to interpret line 3 not as a command but as setting
> the variable name=b.   Get it?   If could explicitly state the command
> that is implied by 'name=b' in the shell, then I think I could do what
> I want.
>
>
> Thanks,
> Peter
>
>
> On Nov 19, 2007 1:29 PM, Robert Arkiletian <robark at gmail.com> wrote:
> > On Nov 19, 2007 9:59 AM, Peter Hartmann <ascensiontech at gmail.com> 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'm not sure exactly what your trying to do but if you  just want to
> > open a file and read names into an array try this
> >
> > #!/usr/bin/python
> > f = file('/path/to/ffirst','r')
> > mylist=[]
> > for x in f.readlines():
> >    mylist.append(x)
> > print mylist
> >
> >
> > note there must be a tab or 4 spaces before mylist.append(x)
> >
> > --
> > Robert Arkiletian
> > Eric Hamber Secondary, Vancouver, Canada
> > Fl_TeacherTool http://www3.telus.net/public/robark/Fl_TeacherTool/
> > C++ GUI tutorial http://www3.telus.net/public/robark/
> >
> > _______________________________________________
> > K12OSN mailing list
> > K12OSN at redhat.com
> > https://www.redhat.com/mailman/listinfo/k12osn
> > For more info see <http://www.k12os.org>
> >
>




More information about the K12OSN mailing list