ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 *
do
fnamearray[$index]=$fname
stat -c %n,%U,%x,%y $fname > $TEMPFILE
fnamen=$(awk -F"," '{ print $1 }' $TEMPFILE) # File name
fnameu=$(awk -F"," '{ print $2 }' $TEMPFILE) # User name
fname=$(awk -F"," '{ print $3 }' $TEMPFILE) # Access time
fname[3]=$(awk -F"," '{ print $4 }' $TEMPFILE) # Modification time
let 'index+=1'
echo ${fnamearray[$index]} >> $TEMPFILE2
done
echo ${fnamearray[0]} # 1st file name
echo $(!fnamearray[1]) # Should be element 1 (user name)?
Maybe what I'm trying to do is not yet implemented in Bash? If not,
what is the proper syntax/construction to dereference an array variable indirectly?
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
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,
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
TEMPFILE2=tempfile2
for fname in *
do
fnamearray[$index]=$fname
stat -c %n,%U,%x,%y "$fname" > $TEMPFILE
fnamen=$(awk -F"," '{ print $1 }' $TEMPFILE) # File name
fnameu=$(awk -F"," '{ print $2 }' $TEMPFILE) # User name
fname=$(awk -F"," '{ print $3 }' $TEMPFILE) # Access time
fname[3]=$(awk -F"," '{ print $4 }' $TEMPFILE) # Modification time
let 'index+=1'
echo ${fnamearray[$index]} >> $TEMPFILE2
done
echo ${fnamearray[0]} # 1st file name
echo "${!fnamearray[1]}" # Should be element 1 (user name)?
But I don't understand what you are trying to to in the last line. Suppose that ${fnamearray[1] is "poodle.jpg".
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 "))
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.