Simple list file ls to an array


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple list file ls to an array
# 1  
Old 07-12-2010
Simple list file ls to an array

Hi all,
Simple question, how can I simply create an array from listing the files in a directory i..e

Code:
myvar=`ls`;
echo $myvar

gives a fulllist of files/ directories

how can I now convery myvar to an array so I can loop around and read each file/dir?

Thnaks

CFSmilie

Last edited by pludi; 07-12-2010 at 09:25 AM.. Reason: code tags, please...
# 2  
Old 07-12-2010
Quote:
Originally Posted by cyberfrog
how can I simply create an array from listing the files in a directory
Bash:

Code:
array=( $( ls . ) )

# 3  
Old 07-12-2010
I seem to get this error:

/usr/bin/ksh: syntax error: '(' unexpected

?
# 4  
Old 07-12-2010
I think that kind of array syntax is ksh93.

You could try the old-fashioned way:

Code:
$ touch File1 File2 File3
$ set -A array *
$ echo ${array[0]}
File1

Useless if your filenames have spaces.
# 5  
Old 07-12-2010
Quote:
Originally Posted by cyberfrog
so I can loop around and read each file/dir?
If your final goal is to loop through the file/dir then probably you don't need an array.
Code:
for file in *
do
 echo $file
done

Again, its just a suggestion.
# 6  
Old 07-12-2010
Quote:
Originally Posted by anchal_khare
If your final goal is to loop through the file/dir then probably you don't need an array.
Code:
for file in *
do
 echo $file
done

Again, its just a suggestion.

If you double quote "$file" after the echo command, that loop can handle any file name.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 7  
Old 07-13-2010
cheers guys, nice back to basics reminder, yep it was being in a different shell the key!

---------- Post updated at 09:50 AM ---------- Previous update was at 04:22 AM ----------

Does anyone know what the syntax is for borune shell? seems only one that is missing and it doesn't recognise any of these commands for convert a variable to an array
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Pass an array to awk to sequentially look for a list of items in a file

Hello, I need to collect some statistical results from a series of files that are being generated by other software. The files are tab delimited. There are 4 different sets of statistics in each file where there is a line indicating what the statistic set is, followed by 5 lines of values. It... (8 Replies)
Discussion started by: LMHmedchem
8 Replies

2. Shell Programming and Scripting

Compare file to array, replace with corresponding second array

ok, so here is the issue, I have 2 arrays. I need to be able to create a loop that will find ${ARRAY1 in the text doc, and replace it with ${ARRAY2 then write the results. I already have that working. The problem is, I need it to do that same result across however many items are in the 2... (2 Replies)
Discussion started by: gentlefury
2 Replies

3. Shell Programming and Scripting

Simple list question

Hi, Can someone tell me how to do this... I want to concatenate a list of files with a ':' using the find command... I have tried find ${dir} -type f -print | sed 's/$/:/g' but what this does is only append a ':' to the end of the file instead of removing the new line and... (5 Replies)
Discussion started by: muay_tb
5 Replies

4. Shell Programming and Scripting

Output file list to array?

Hey, guys, scripting newb here. I'm trying to get a list of all .dmg files in a folder and save the output into an array. My first attempt was ARRAY= ( `ls $REIMAGEPATH | grep \.dmg$` ) However, I understand why that doesn't work now (at least I think I do). But I don't know what the... (5 Replies)
Discussion started by: nextyoyoma
5 Replies

5. Shell Programming and Scripting

Array in Perl - Detect several file to be in one array

Hi everyone I have one question about using array in perl. let say I have several log file in one folder.. example test1.log test2.log test3.log and the list goes on.. how to make an array for this file? It suppose to detect log file in the current directory and all the log file will... (3 Replies)
Discussion started by: sayachop
3 Replies

6. Shell Programming and Scripting

simple array problem

Hello experts, I need help in my code. I have an input file like this: 100814 1205 1724127 7451382 -10 00:30:1b:48:92:3a 100814 1206 1724127 7451382 -72 00:30:1b:48:92:3a 100814 1207 1724127 7451382 -72 00:30:1b:48:90:3b 100814 1208 1724127 7451382 -72 00:30:1b:48:92:3a 100814 1209... (12 Replies)
Discussion started by: enes71
12 Replies

7. Shell Programming and Scripting

How to get list of user into an array..

Hi, cut -d: -f1,3 /etc/group >rpt.out I have a doubt in above unix commands. right i am getting list of group user id into rpt.out file. instead i need to store it as an array. could you please tell me how can i get list of user into an array.. If u could tell me give me in perl script... (2 Replies)
Discussion started by: solo123
2 Replies

8. UNIX for Dummies Questions & Answers

simple way to read an array in ksh

hi, I'm a newbie to shell scripting. I wanted to initialise an array using basic for loop and read it. Then i want to print it as a .CSV file.. Any help would me much appreciated.. (1 Reply)
Discussion started by: pravsripad
1 Replies

9. Shell Programming and Scripting

storing values in a list or array

i have a file called file.txt having the following entries. 2321 2311 2313 4213 i wnat to store these values in a list and i want to iterate the list using loop and store it in another list (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies

10. UNIX for Dummies Questions & Answers

Simple Array in Ksh Scripting

Ksh Scripting Can some one give me a simple example of array operations using ksh. For Ex: week_array = {Sunday Monday Tuesday Wednesday Thursday Friday Saturday} I want to assign and retrieve and print them along with their index. I am looking for the o/p like: 0 Sunday 1 Monday ... (2 Replies)
Discussion started by: ravikirankethe
2 Replies
Login or Register to Ask a Question