For debugging purposes you can use the command set -xv
(you can put it under the #!/bin/bash
).
For the script not writing any .htaccess I think the culprit is in your if statement:
your conditions state that if .htaccess exists and does not contain "AddHandler cgi-script" then you can do something, but if the file .htaccess does not exists the if condition exit without doing anything
try this:
if [[ ! -e "$FOLDER/.htaccess" || ! $(grep "AddHandler cgi-script" "$FOLDER/.htaccess") ]]
So if the file exists it will add the "AddHandler cgi-script" and if the file doesn't exist it will create it with the requested string