Reputation: 653
In order to use chown
command I need to know who's running my script. Real name because it's going to be run with sudo. How can i do it?
Upvotes: 0
Views: 2857
Reputation: 21
#!/bin/bash
# Init
FILE="/tmp/out.$$"
GREP="/bin/grep"
#....
# Make sure only root can run our script
if [ "$(id -u)" != "0" ]; then
echo "This script must be run as root" 1>&2
exit 1
fi
# ...
Upvotes: 2
Reputation: 777
For completeness, there is also id -un
and whoami
but env vars should probably be preferred unless you have a reason not to.
Upvotes: 3