LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-17-2006, 12:31 PM   #1
sixerjman
Member
 
Registered: Sep 2004
Distribution: Debian Tesing / Unstable
Posts: 166
Blog Entries: 1

Rep: Reputation: 30
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 *
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?
 
Old 10-18-2006, 09:01 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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
Wednesday October 18 14:57:28 BST 2006


$ set -- $(date)
$ echo $*
Wednesday October 18 14:57:39 BST 2006

# let's reverse it!
$ echo $6 $5 $4 $3 $2 $1
2006 BST 14:57:39 18 October Wednesday
 
Old 10-18-2006, 01:53 PM   #3
ygloo
Member
 
Registered: Aug 2006
Distribution: slack
Posts: 323

Rep: Reputation: 30
what is "!" for

echo $(!fnamearray[1]) # Should be element 1 (user name)?
 
Old 10-25-2006, 10:07 AM   #4
sixerjman
Member
 
Registered: Sep 2004
Distribution: Debian Tesing / Unstable
Posts: 166
Blog Entries: 1

Original Poster
Rep: Reputation: 30
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
 
Old 10-25-2006, 10:23 AM   #5
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,287

Rep: Reputation: 173Reputation: 173
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:
(IFS=:;set -- $PATH;for x;do echo $x;done)
 
Old 10-25-2006, 11:02 AM   #6
sixerjman
Member
 
Registered: Sep 2004
Distribution: Debian Tesing / Unstable
Posts: 166
Blog Entries: 1

Original Poster
Rep: Reputation: 30
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.
 
Old 10-25-2006, 11:18 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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 "))

Last edited by jschiwal; 10-25-2006 at 11:27 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash scripting: how can I reference a variable? frankie_DJ Programming 4 10-06-2006 05:13 AM
Bash Local Variable Recursion With Array jshivers Programming 0 06-16-2006 04:31 PM
finding array length from a reference (in Perl) lowpro2k3 Programming 1 06-22-2005 04:49 PM
reference Variable in C++ atul_mehrotra Programming 6 10-06-2004 10:49 AM
Variable Array in C mojozoox Programming 6 12-16-2003 01:39 AM


All times are GMT -5. The time now is 03:08 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration