There is absolutely nothing that is seen by two minds simultaneouslyBertrand Russell
Relates to Apache and UNIX, OSX
Earlier I was setting up a couple of shell scripts to allow me to quickly switch between PHP versions on the Apache web server and enable/disable my unit testing environment in OS X. Along the lines of:
apachectl stop
httpd -f /usr/local/php5/httpd.conf
Since such scripts must be run as root user I wanted to concote a little script to check I had used sudo to run the commands (as I have a habit of forgetting!). Firstly I came up with the following:
#!/bin/sh
# verify-su
umask 222
touch /tmp/user-status
echo `whoami` 2> /dev/null >| /tmp/user-status
if [ $? -ne 0 ] ; then
if [ $# -eq 0 ] ; then
"This command must be run with root privileges!"
else
"$1 must be run with root privileges!"
fi
exit 1
fi
\rm /tmp/user-status
exit 0
A file is created with read only permissions (by temporarily changing umask) and then the script
attempts to write to it. The output redirection to file will only succeed if the script is run as root (the superuser).
In running this without the rm command I discovered that a script executed with sudo will return root from the whoami command instead of the user name I am currently logged in with. So this could actually be simplified considerably to:
if [ `whoami` != 'root' ] ; then
# etc …
exit 1
else
exit 0
fi
There is always a simpler way to do things!
Posted on Monday, Jun 20, 2005 at 19:12:51.
Comments on Verify Sudo in Script Execution (1)
α comment
If you arn't worried about the exit code if the user isn't root (e.g.it could be 232 instead of 1) you could do:
otherwise:
Posted by Anonymous
Friday, Aug 12, 2005 at 22:42:30