Output file list to array?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output file list to array?
# 1  
Old 04-15-2012
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

Code:
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 best method is to do this. Some have suggested using a for loop, others using find. But in either case, I can't figure out how to pass the input to an array.

In case it helps, the purpose of the full script is to find all .dmg images in a folder, partition a hard drive with that number of partitions, and restore each image onto the corresponding partition. I've worked out most of it, but the lynchpin is being able to generate the list in the first place! Thanks!

Last edited by nextyoyoma; 04-15-2012 at 12:35 PM..
# 2  
Old 04-15-2012
Hi, and welcome. here are some pointers:
  • There should be no space behind the =-sign
  • Use double quotes around the variable reference
  • Use single quotes around the grep string
  • If your shell supports it, I recommend using $(...) instead of `...`
  • This will work as long as there are not spaces or special characters in the file names (and there are more straightforward methods)

Last edited by Scrutinizer; 04-15-2012 at 12:59 PM..
# 3  
Old 04-15-2012
Code:
ARRAY=(*.dmg)

# 4  
Old 04-15-2012
Thanks for the tips. I got that working, but now I have another problem. I'm trying to get the size of each file, but when I try to use
Code:
stat -c %s <file>

I get the message
Code:
stat: illegal option -- c

what is going on here?
# 5  
Old 04-15-2012
Hi, nextyoyoma, if you got a different problem, you need to play your cello in a fresh new thread. Smilie
# 6  
Old 04-15-2012
haha, ok.
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

How to list files names and sizes in a directory and output result to the file?

Hi , I'm trying to list the files and output is written to a file. But when I execute the command , the output file is being listed. How to exclude it ? /tmp file1.txt file2.txt ls -ltr |grep -v '-' | awk print {$9, $5} > output.txt cat output.txt file1.txt file2.txt output.txt (8 Replies)
Discussion started by: etldeveloper
8 Replies

3. UNIX for Dummies Questions & Answers

Output a list of five books with their filename titles into one file

Dear unix forum, could I output a list of five books with their file name titles into one file? In order o output all the contents of all the files with their file names there was: find . -type f | while read x; echo -e "\n$x";cat "$x";done > бетховен.txt In spite of them being successively... (5 Replies)
Discussion started by: Xcislav
5 Replies

4. Homework & Coursework Questions

Loop to Convert a list from an input file and output it to another file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: A) Write a script, which will take input from a file and convert the number from Centigrade to Fahrenheit... (5 Replies)
Discussion started by: AliTheSnake
5 Replies

5. Shell Programming and Scripting

How to process select list of files and output to the same file?

Hi, I've a list of files ac_info.tps, subscription_array.tps, .......and many other files one of the file, bin_range_list.tps has the following content CREATE OR REPLACE TYPE "BIN_RANGE_LIST" AS TABLE OF BIN_RANGE_ELEM; / grant execute on... (4 Replies)
Discussion started by: jediwannabe
4 Replies

6. Shell Programming and Scripting

Match list of strings in File A and compare with File B, C and write to a output file in CSV format

Hi Friends, I'm a great fan of this forum... it has helped me tone my skills in shell scripting. I have a challenge here, which I'm sure you guys would help me in achieving... File A has a list of job ids and I need to compare this with the File B (*.log) and File C (extend *.log) and copy... (6 Replies)
Discussion started by: asnandhakumar
6 Replies

7. Filesystems, Disks and Memory

iostat output vs TPC output (array layer)

Hi Guys, I've been having some arguments with my colleagues about one thing. Always my thought was that as as far as disk performance is concern by looking at the output of the iostat command (AIX) you would be able to identify if you have a hot disk and then by moving some files out that disk... (3 Replies)
Discussion started by: arizah
3 Replies

8. Shell Programming and Scripting

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 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 CF:) (6 Replies)
Discussion started by: cyberfrog
6 Replies

9. Shell Programming and Scripting

Creation of output file from list of file

Hi Guru's, Eventhough I know basic shell scripting, Iam not an expert. Can any one help me to get a logic/answer for the below requirement: I've to create an output file "outputfile.txt" from many datafiles (ex: abc.dat, xyz.dat). Header record layout for "outputfile.txt" should be... (7 Replies)
Discussion started by: ganapati
7 Replies

10. Shell Programming and Scripting

formating array file output using perl

Hello, I am trying to output the values in an array to a file. The output needs to be formated such that each array value is left jusified in a field 8 character spaces long. Also, no more than 6 fields on a line. For example: @array= 1..14; Needs to be output to the file like so: 1 ... (4 Replies)
Discussion started by: seismic_willy
4 Replies
Login or Register to Ask a Question