Reputation: 13
I want to find all ".htaccess" files (if the file exist, if not create one) for all user accounts in a whole string of possible sub-directories underneath "public_html" (but not public_html). Basically find it in anything that remotely resembles a directory that may contain images.
Here is my code:
#!/bin/bash
cat /etc/domainusers | cut -f1 -d: | while read USER; do
find /home/$USER/public_html/ -type d -regex ".*\/public_html\/.*\(img.\|pic.\|pics.\|image.\|images.\|Image.\|Images.\|Pic.\|Pics.\|Img.\|picture.\|pictures.\|Picture.\|Pictures.\|upload.\|Upload.\|download.\|Download.\|Gallery\|gallery\|Galleries\|galleries\|import.\|Import.\|Thumbnail.\|Thumbnails.\|thumbnail.\|thumbnails.\|wpThumbnail.\|thumb.\|Thumb.\)" | while read FOLDER;
do
if [[ -e "$FOLDER/.htaccess" && ! $(grep "AddHandler cgi-script" "$FOLDER/.htaccess") ]]; then
echo -e "AddHandler cgi-script .php .pl .py .jsp .asp .htm .html .shtml .sh .cgi .php5 .php4 .php3 .phps .txt .bat .cmd .rb .txt .msi .exe .xml .xhtml\nOptions -ExecCGI -Indexes" >> "$FOLDER/.htaccess"
chattr +ia "$FOLDER/.htaccess"
fi
done
done
Although the above script runs ok, it does not actually do what I want it to do. I checked a user's image directory and didn't find any ".htaccess" file there. :/
Ideally, I would also like to see on the screen what it is going on during the execution, and, if possible, also output the result to a text file.
Upvotes: 1
Views: 1167
Reputation: 46
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
Upvotes: 1