-
Notifications
You must be signed in to change notification settings - Fork 1
/
owner-set
executable file
·49 lines (36 loc) · 1.46 KB
/
owner-set
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env bash
#---------------------------------------------------------------------
usage ()
{
cat <<EOT
${0##*/}
Sets the filesystem owner and group recursively from the project
root folder. Uses the provided command line arguments if present,
otherwise defaults to the owner and group set for the project
root folder. Should be run from the project root folder.
Usage:
bin/${0##*/} [owner] [group]
EOT
exit ${1:-0} # Exit with code 0 unless an arg is passed to the method.
}
if [ "$1" = '-h' ]; then
usage
fi
DIR="$( cd -P "$( dirname "$0" )"/.. >/dev/null 2>&1 && pwd )"
# Input processing.
if [ -n "$1" ]; then
ARG_OWNER=$1
else
# This works around the scripts being run elevated via `sudo`.
# Can't use `stat -c'%U %G'|-f '%sU %sG' ./` because options are not standard across systems.
#@TODO: This method doesn't work when $DIR is a symlink with a name that doesn't match the target. Case study: eu.loadsysdev.com, the ~/public_html/ folder is really a symlink to /var/www/sites/eu.loadsysdev.com/html, and the owner of the symlink is root:root.
ARG_OWNER="$( \ls -l "${DIR}/.." | grep -v '^l' | grep " $(basename $( pwd ))\$" | awk '{ print $3 }' )"
fi
if [ -n "$2" ]; then
ARG_GROUP=$2;
else
ARG_GROUP="$( \ls -l "${DIR}/.." | grep -v '^l' | grep " $( basename $( pwd ))\$" | awk '{ print $4 }' )"
fi
# Make the changes.
echo "## Setting file ownership for root folder to: ${ARG_OWNER}:${ARG_GROUP}";
chown -R ${ARG_OWNER}:${ARG_GROUP} $DIR