LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 12-12-2005, 07:46 PM   #1
tigeraven
LQ Newbie
 
Registered: Sep 2005
Location: Seattle
Distribution: Debian 3.1
Posts: 5

Rep: Reputation: 0
Bash files > array


Hi,
I would like to put the contents of a dir into an array within a bash script. So I can be able to perform an operation on each element. I am having difficulty finding any source of info on how to do this. Can anyone tell me how to do this?
 
Old 12-12-2005, 08:36 PM   #2
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
well, i'm not exactly sure how to put them into an array, but the following will print all the files in the directory.
[code]
#! /bin/bash

for $file in *
do
echo "$file"
end
[\code]

so you could easily replace the echo statement to do something to each element, or add each element to an array.
 
Old 12-13-2005, 12:02 AM   #3
dugas
Member
 
Registered: Jul 2004
Location: louisiana
Distribution: fedora 4/kubuntu
Posts: 116

Rep: Reputation: 15
counter=0
arrayName=[]

for i in *
do
arrayName[$counter]=$i
counter=`expr $counter+1`
done

for ((i=0;i<$counter;i++))
do
echo ${arrayName[$i]}
done

exit
 
Old 12-13-2005, 03:40 AM   #4
tigeraven
LQ Newbie
 
Registered: Sep 2005
Location: Seattle
Distribution: Debian 3.1
Posts: 5

Original Poster
Rep: Reputation: 0
That does the trick, thanks man
 
  


Reply



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 shell script split array robertngo Programming 13 06-19-2011 11:01 PM
Bash Script Array index value Kedelfor Programming 10 04-29-2009 04:37 AM
bash: put output from `ls` into an array samel_tvom Programming 18 07-31-2005 12:55 AM
Bash 2 dimensional array ldp Linux - General 5 07-29-2005 11:29 AM
array in bin/bash how to x2000koh Programming 12 05-09-2003 04:51 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:47 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration