Reputation: 10173
I have a makefile with an install
target which creates a bunch of directories and installs some files. I would also like to check for the existence of a group and if it doesn't exist, create it. To do this I need to make sure the user is running as root. Is there a preferred way to do this in a makefile?
install: afile
# Check user is root, otherwise print error and exit.
# Check if "auser" exists, otherwise create it.
# Check if "agroup" exists, otherwise create it.
install -d -o auser -g agroup -m 0755 /path/to/stuff
install -o auser -g agroup -m 0644 afile /path/to/stuff/afile
service start aservice
Upvotes: 0
Views: 134
Reputation: 100781
I don't see what this has to do with make, really, but I like to use the id
command for doing these types of checks:
[ `id -u` = 0 ] || { echo "Not running as root"; exit 1; }
id "auser" 2>/dev/null || { echo "No user 'auser'"; exit 1; }
Checking groups is trickier; I'm actually not sure of a program that will do it. Of course, you can always just check /etc/group
directly:
grep -q "^agroup:" /etc/groups || { echo "No group 'agroup'"; exit 1; }
Upvotes: 1