Having some trouble with a while loop in bash script : I'm trying to get the variables setup properly in my script. Basically the x and y variables should increment by 5 after each iteration of the loop. The a value should increment by one, and the loop should iterate $numResults amount of times.
    
   

 The numResults variable is working fine and the loop does try to iterate that number of times. I'm encountering 4 sets of errors since that's the number stored in that variable. For my other variables, when I replace them with real numbers, then the program runs fine but just not with the variables in place.
    

    #!/bin/bash
    
    # An application that for each course with more than 50 students enrolled generates 
    # an advisory report to warn of over-enrollment. It generates a text file from a user-defined 
    # template which uses the following variables:
    # 
    # • [[dept_code]]
    # • [[dept_name]]
    # • [[course_name]]
    # • [[course_start]]
    # • [[course_end]]
    # • [[credit_hours]]
    # • [[num_students]]
    # • [[course_num]] (the course number as specified in the filename of the .crs file)
    # • [[date]]
    #
    # Create a new user-specified subdirectory for files mkdir -p "$4"
    
    numResults=`grep -rE --include \*.crs -e "^[5][1-9]$" -e "^[6-9][0-9]$" -e "^[1-9][0-9][0-9]$" ./data | gawk '{n++} END { print n }'`
    
    let i=0; x=1 y=5 a=1 while [[ $i<="$numResults" ]] do
    # Gets the course code and number to use as the output file name && stores course_num to the output file temporarily for retrieval file=`grep -rE --include \*.crs -e "^[5][1-9]$" -e "^[6-9][0-9]$" -e "^[1-9][0-9][0-9]$" "$1" | sed -E 's/.*([A-Z]{3})([0-9]{4})\.*crs:[0-9][0-9]/\1\2/g' | gawk 'NR==$a { print; echo "" }'` course=`echo $file | sed 's/[A-Z]//g'` (cat "$2"; echo ""; echo "$course";) > "$4/$file"
    
    
    # Locate all files in the data directory with the .crs extension that also contain a line with 
    # only a string "51" - "999" and grabs the contents of that file and stores them to the new 
    # output file temporarily for retrieval grep -hrEB 4 --include \*.crs -e "^[5][1-9]$" -e "^[6-9][0-9]$" -e "^[1-9][0-9][0-9]$" "$1" | gawk 'NR==$x,NR==$y{print}' >> "$4"/$file
    
    
    # Create variables to hold the dept and course info varCNum=`gawk -v n=6 '{saved[FNR % n] = $0} ENDFILE {if (FNR >= n) print saved[(FNR + 1) % n]}' "$4"/$file`
    
    varDC=`gawk -v n=5 '{saved[FNR % n] = $1} ENDFILE {if (FNR >= n) print saved[(FNR + 1) % n]}' "$4"/$file` varDN=`gawk -v n=5 '{saved[FNR % n] = $0} ENDFILE {if (FNR >= n) print saved[(FNR + 1) % n]}' "$4"/$file | gawk '{for(i=2;i<=NF;i++){printf "%s", $i};}'`
    
    varCN=`gawk -v n=4 '{saved[FNR % n] = $0} ENDFILE {if (FNR >= n) print saved[(FNR + 1) % n]}' "$4"/$file` varCS=`gawk -v n=3 '{saved[FNR % n] = $2} ENDFILE {if (FNR >= n) print saved[(FNR + 1) % n]}' "$4"/$file` varCE=`gawk -v n=3 '{saved[FNR % n] = $3} ENDFILE {if (FNR >= n) print saved[(FNR + 1) % n]}' "$4"/$file` varCH=`gawk
    -v n=2 '{saved[FNR % n] = $0} ENDFILE {if (FNR >= n) print saved[(FNR + 1) % n]}' "$4"/$file`
    
    varNS=`gawk -v n=1 '{saved[FNR % n] = $0} ENDFILE {if (FNR >= n) print saved[(FNR + 1) % n]}' "$4"/$file`
    
    
    # Substitute the variables into the output file && and clean up sed -E "s/\[\[ dept_code \]\]/$varDC/g; s/\[\[ dept_name \]\]/$varDN/g;  s@\[\[ course_name \]\]@$varCN@g; s@\[\[ course_start \]\]@$varCS@g; s@\[\[ course_end \]\]@$varCE@g; s@\[\[ credit_hours \]\]@$varCH@g; s@\[\[ num_students \]\]@$varNS@g;  s@\[\[ course_num \]\]@$varCNum@g; s@\[\[ date \]\]@$varDate@g;" "$4"/$file | sed -e :a -e '$d;N;2,6ba' -e 'P;D'
    
    let i+=1 let x+=5 let y+=5 let a+=1
    
    done

   

 I'm passing in the following arguments to my script:
    $ ./data assign4.template 12/16/2021 ./output
    
    Which gives the following error message "./assign4.sh: line 31: ./output/: Is a directory
    ./assign4.sh: line 37: ./output/: Is a directory
    gawk: warning: command line argument `./output/' is a directory: skipped"