Bash indirect reference to array variable(s)
I'm trying to programmatically create and reference arrays which
are based on the files in a directory. The arrays are created using a 'for' loop, that's not a problem. The difficulty arises in referencing each array after creation. I tried creating a separate file which contains the file names, then accessing each array by filename (either directly or indirectly), but niether is working. Consider: Code:
for fname in * what is the proper syntax/construction to dereference an array variable indirectly? |
you may find this idiom a bit cleaner rather than all that
awk tempfile nonsense. (I am using date, as no 'stat' on solaris) you can use set to extrapolate to the numbered parameters Code:
$ date |
what is "!" for
echo $(!fnamearray[1]) # Should be element 1 (user name)? |
THANK YOU! Yes, the awk/TEMPFILE is ugly and unnecessary, but it was my first try at the script and I was stuck. I always have the feeling that the more ugly and unwieldy the code I'm writing is, the more likely there's a much easier way to do it. Thanks again! :-D
|
very very true.
uglyness is KEY. If my code starts getting ugly, or complicated I seriously think about re-doing it. It's a cool technique the set, very few people know of it. remember that set technique will clobber your existing parameters unless you put in a function or a subshell, try this to show your PATH: Code:
|
ygloo, the "!" notation in that context is an attempt to indirectly reference a variable via the contents of another variable. I solved the problem by abandoning the indirect variable stuff and just assigning a unique variable for each file name called "$fname.stat" in the "for" loop.
|
I made some minor corrections to your original script:
Code:
TEMPFILE=tempfile The indirection will expand to "echo ${poodle.jpg}". ---- To create an array of the files in the current directory you could do something like this: Code:
fnamearray=($(find ./ -maxdepth 1 -type f -printf "%f ")) |
All times are GMT -5. The time now is 03:48 PM. |