Parsing Directory Names for Use as Bash Variables

Hi all:
I have a directory where all of the subdirectories are named by the convention "images_#1:#2_Date." My goal is to get an array for each subdirectory that has the structure (#1,#2, int). I am able to use awk to print each subdirectory's values, but cannot figure out how to get them into an array. My script can be seen below:

#! /bin/bash
IFS=""
ls -f|sed 's// /'|sed 's// /'|sed 's/:confused: /'|awk '{print $2,$3}'|sort

This gives output of:
501 12
502 14, etc.

I attempted:

#! /bin/bash
cd images
IFS=""
my_array=$(ls -f|sed 's// /'|sed 's// /'|sed 's/:confused: /'|awk '{print $2}'|sort)
echo ${my_array[5]}

But this just outputs the entire list with a '[5]' at the end. I am at a loss for how to proceed. Any help you can provide would be greatly appreciated. Thanks.

Bash didn't seem to like passing the variables out of a loop but here's something with ksh.

#! /bin/ksh
# Get the listing and loop thru to add to the array.
cnt=0
ls -1 *Date |sed -e 's/_/ /g' -e 's/:/ /' | awk '{print $2,$3}'|sort | while read line
do
  if [ ! -z "$line" ]
  then
    myarray[$cnt]="$line"
    cnt=$(($cnt+1))
  fi
done
# print out the array.
ncnt=0
while [ $ncnt -lt $cnt ]
do
  echo "My Array: ${myarray[$ncnt]}"
    ncnt=$(($ncnt+1))
done

Hope it helps

This does not appear to be working. When I ran it as in the post above, I simply got no input. I inserted a second "if" loop to see if, in fact, the lines were being read. They are not. With the current version of the script, seen below, the output is:
>
No length
No length
>

This seems odd, because there are currently 39 directories, and the output of the first command, when run on its own, has 41 lines (the 39 directories plus lines for "./" and "../" Any ideas as to what could be causing this? Thanks again.

A bit of a change from the original intent, right now I am just trying to get an array of the first number in the directory, and I had the format slightly wrong. The directories are actually: images_#1_#2_Date/. I figure if I can get the first number working, getting the second into the array should be trivial.

Sorry, the indentation does not seem to be working.

If I'm reading this correctly, your directories would look something like this.

If this is true:
Try this:

#! /bin/ksh
# cd images
cnt=0
find ./ -name \*Date -type d | sed -e 's/_/ /g' -e 's/:/ /g'| awk '{print $2,$3}'| sort | while read line
do
  if [[ ! -z "$line" ]]
  then
    myarray[$cnt]="$line"
    cnt=$(($cnt+1))
  fi

  if [[ -z "$line" ]]
  then
    echo "No length"
  fi
done

ncnt=0
while [ $ncnt -lt $cnt ]
do
  echo "My Array: ${myarray[$ncnt]}"
  ncnt=$(($ncnt+1))
done 

if not, AND they all begin with images then substitute the

with

You really should have something to make sure you only get the directories you want and not the . and .. directories as well. They will show up as No Length.

Hope this helps

Looks like it should work, but I won't be able to check it for at least a few days. Thanks again for the help.

A solution with bash (directories names in the form images_#1_#2_Date/) :

~/images> ls
array.sh          images_1_3_Date/  images_2_3_Date/  images_3_3_Date/  images_4_3_Date/  images_5_3_Date/    invalid_8_9_Date/
images_1_1_Date/  images_2_1_Date/  images_3_1_Date/  images_4_1_Date/  images_5_1_Date/  images_6_7_8_Date/
images_1_2_Date/  images_2_2_Date/  images_3_2_Date/  images_4_2_Date/  images_5_2_Date/  images_Date/
~/images> cat array.sh
numbers=$(ls -p | awk -F'[_/]' '/^images_.*_Date\/$/ && NF==5 {print $2,$3,$0}')

declare -a Array1=( $(echo "$numbers"|awk '{print $1}') )
declare -a Array2=( $(echo "$numbers"|awk '{print $2}') )

for ((i=0; i<${#Array1[*]}; i++))
do
   echo "subdir images_${Array1[$i]}_${Array2[$i]}_Date"
done
~/images> array.sh
subdir images_1_1_Date
subdir images_1_2_Date
subdir images_1_3_Date
subdir images_2_1_Date
subdir images_2_2_Date
subdir images_2_3_Date
subdir images_3_1_Date
subdir images_3_2_Date
subdir images_3_3_Date
subdir images_4_1_Date
subdir images_4_2_Date
subdir images_4_3_Date
subdir images_5_1_Date
subdir images_5_2_Date
subdir images_5_3_Date
~/images>

Jean-Pierre.

mph: I still can't get yours to work, but thanks a lot for the attempts.

aigles: That works beautifully. Thank you very much.