What this while loop is doing?

Can anyone please explain what this code is doing for first 6 lines? I believe this is array but I couldn't understand what exactly it is doing.

set -A Dis_array 95
typeset -L2 distribution
let count=0
while (( $count < ${#Dis_array[*]} )) ; do
       distribution=${Dis_array[count]}
       echo "distribution = ${distribution}"

       cat  ${Filename}    >> ${SOME_PATH}/data/${distribution}abcfile.${log_time}
       rc=$?
       if [ ${rc} != "0" ]
       then
          echo "Return Code is ${rc}"
          exit ${rc}
       fi

       echo "${distribution}abcfile sent to DC ${distribution} system"

      let count="count + 1"

done


find /some/other/path/data -follow -name "*abcfile*" -mtime +2 -print -exec rm {} \;

Please use code tags, not icode tags, that is the button.

First it declares Dis_array as an array, set -A is how it's done in KSH -- other shells use ARRAY=( value1 value2 value3 ).

It loops while while $count is less than the number of elements in the array, ${#Dis_array[]}. It looks like gibberish the first time you see something like that, but it's similar to the way commandline arguments are handled -- $ for "all arguments", ${array[]} for "all elements", $# for the number of arguments, ${#array[]} for the number of elements. Actually I'd use ${#array[@]} since that doesn't rely on splitting.

What I don't understand is where any elements in the array are coming from. It looks like it's setting it to exactly one value -- 95. Perhaps they didn't really need to use an array here. All they're using is the first element.

1 Like

Sorry I am new to this forum, will use code tag going forward. Thanks for your reply!

If nothing else is done with $count I would prefer a simpler for loop here:

for distribution in ${Dis_array[@]}
do
  echo "distribution = ${distribution}"
  ...
done