How to check if i'm root in a script?
Hi there i wanted to make it easier for me to install things by making a script of repetitive or long commands. I was just wondering if there is a way of checking if i'm root. If i am logged in as root then to continue to the next commands and if i'm not then to stop and warn me? I was thinking of something like passing the output of "whoami" to a variable but I dont know how to do this. Is there a more elegant way to do this? or is there a command already out there which does this?
Any help would be appreciated. |
Well, if the file is owned by root then can't you just chmod the file so that only the owner of the file can execute it?
Or am I missing something? |
Hell, you could, say, do something like:
Code:
whoami > /home/username/whoami |
Code:
[ $(whoami) == "root" ] Code:
if [ $(whoami) == "root" ]; then A good compromise if you want to have a script for a user with optional root commands, you could use that if statement and instead of warning a user that you aren't root then you could actually make a separate script with the root commands and call it from that script with `su -c "rootscript"` that way it would ask for a password. It seems crude but it'll work. There are certainly better solutions but I'm not an expert. Good luck! |
Great, just what i was after, thanks for the prompt help guys/gals. Much appreciated!
|
All times are GMT -5. The time now is 07:48 AM. |