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 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!

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)
ARRAY=(*.dmg)

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

stat -c %s <file>

I get the message

stat: illegal option -- c

what is going on here?

Hi, nextyoyoma, if you got a different problem, you need to play your cello in a fresh new thread. :wink:

haha, ok.