I'm trying to write up a little helper script that will change permissions and ownership to some sites on a server.
Right now, I can either pass in 1 site, or do all via simply skipping that argument.
What I am finding is that I need to be able to apply to multiple sites on the server, but not all, so I tried making an attempt at passing an array via:
SLIST=("my.site.com" "your.site.com")
./website-perms 644 755 kevin "${SLIST[@]}"
However, it only does the first item in the array my.site.com
How do I fix this so I can pass in an array of sites?
FULL CODE
#!/bin/bash
# Done nightly, and copied to /usr/bin/
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
if [ $# -eq 0 ]; then
echo "usage: ./website-perms.sh fileperm folderperm owner (1|2) (1=kevin,2=www-data) (optional) Site Array"
exit 1
fi
function chg_perms() {
echo "find $1 -type f -exec chmod $2 {} \\";
echo "find $1 -type d -exec chmod $3 {} \\";
find $1 -type f -exec chmod $2 {} \;
find $1 -type d -exec chmod $3 {} \;
echo "-- chg_perms done";
}
function chg_owner() {
echo "chown -R $2:www-data $1";
chown -R $2:www-data $1;
echo "-- chg_owner done";
}
SITES=$4;
if [ -z $SITES ]; then
for dir in /var/www/httpdocs/*/
do
dir=${dir%*/}
chg_perms "/var/www/httpdocs/${dir##*/}" $1 $2
chg_owner "/var/www/httpdocs/${dir##*/}" $3
done;
else
for dir in "${SITES[@]}" #ONLY DOES THE FIRST ITEM
do
chg_perms "/var/www/httpdocs/$dir" $1 $2
chg_owner "/var/www/httpdocs/$dir" $3
done
fi;
$EUID -ne 0
, I would use$(id -n) -ne 0
. Environment variables are not reliable for that test, and EUID is a non-POSIX extension. Try this:EUID=0 bash -c 'echo $EUID'
. And this:sh -c 'echo $EUID'
chmod ug=rwX,o=rX
. Note that's a capital-X, not lowercase. It sets only directories to executable. And if you want all new files created in the dir to be group-owned by the dir's group, that's when you need to run a 2ndchmod
to add setgid to the directories withfind $1 -type d -exec chmod g+s {} +
export
array variables into the environment, so you can't do something likeavar=('foo bar' 'a*b*') ./bash-array-arg.sh avar
to put an array in the environment, and give its name to the called program. The bash manual says "not yet", so a future bash may support it. BTW, when you use that syntax, the resulting env var is a scalar that includes the parens, like if you usedavar='(...)'
. I didn't check zsh.