Vi editor in UNIX hwk HELP!!

i need help with this homework. PLEASE

Assignment:
Create a directory "preFinal" in your home directory. Create empty files "a", "b", "c", etc in directory preFinal in the amount that match the number of directories preFinal*

This the code i have within Vi editor. But it is not working all the way. After runing bash about 10 times the letters jump from i to a random number and not in abc order. PLEASE TELL me how to do this.

#!/bin/bash

dir="preFinal"

if [ -d $dir ]; then
       echo "dir exists, moving..."
       i=0
       for dir_bkp in `ls -lrt | grep "^d" | egrep "preFinal*" | awk '{print $NF}' | sort -nr`; do
               next=$(echo "$dir_bkp" | sed 's/^.*\.//')
               if [ $next == "preFinal" ]; then
                       next=-1
               fi
               next=`echo $next|sed 's/^0*//'`
               ((next++))
               next=$(printf "%03d" $next)
               #next="00$next"
               echo $next
               mv $dir_bkp $dir.$next
               ((i++))

else
       mkdir $dir
       touch $dir/a
fi


Bre/ GSW university / Unix CIS 3200 / Simon

Please fill out the homework template, the course information you've given is incomplete.

I fixed it.

You might want to review the statement in item #2 above. It isn't clear to me that you're adding files to the correct directory.

Are the names of the files really random after the 1st 9, or is there just a jump with some names you expected skipped? Do you think there might be any relationship between the fact that values 61 through 69 worked as expected and the next one failed when you're using "\xdigits" as a format string to printf to create your file names?