Quantcast
Channel: UNIX and Linux Forums
Viewing all articles
Browse latest Browse all 16232

Using nested for loop to iterate over file names

$
0
0
I'm trying to grab a list of file names from a directory, then process those files 5 at a time. In the link below. Instead of using files I'm using the files array which contains 15 strings starting with AAA .

So I'm trying to assign $fileset 5 of the strings at a time to pass to a command. So $fileset looks like this the first pass:

Code:

AAA BBB CCC DDD  EEE
Then the second iteration:

Code:

FFF GGG HHH III JJJ

Code:

files=( AAA BBB CCC DDD EEE FFF GGG HHH III JJJ KKK LLL MMM NNN OOO )

  echo "Total files in array : ${#files[*]}"
  total=${#files[*]}

  for (( i = 0 ; i < $total ; i++ )) do
  fileset=""

      for (( j = $i ; j < 5 ; j++ )) do
        fileset=$fileset${files[$j]}
      done

      echo $fileset
      fileset=""

  done

Moderator's Comments:
Please use CODE tags for sample input and output as well as for code segments.

Viewing all articles
Browse latest Browse all 16232

Trending Articles