You are not logged in.
Pages: 1
I have a Bash script that requires root privileges to its work. I want to bail out of the script if it's not being run as root, lest it try and fail a couple dozen commands. I could just make the script only executable by root, but it got me to wondering if there's any elegant/reliable way for a script to check with what permissions it's being run?
Offline
[[ "${EUID}" = "0" ]] && ( echo "Do not run as root." ; exit 1 )
Offline
I would try using the id command. It tends to exist on every system and root tends to nearly always have uid 0. So you could do something like:
if [[ $(id -u) -ne 0 ]]; then
echo "This script must be run as root."
exit
fi
This will work in bash. You may have to make some adjustments if you are using another shell, but still id -u is probably the right start.
Regards,
j
Offline
A belated thanks, guys.
A question on permissions, why is it that the following happens (my user is UID = 1000):
b-con@beacon:~$ sudo id -u
0
b-con@beacon:~$ sudo echo `id -u`
1000
Why does echo seem change which user is executing the command?
Offline
it's probably just that the command in ticks `id -i` is being resolved before the "sudo echo $something" is run...
Offline
Pages: 1