LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script to check root user? (https://www.linuxquestions.org/questions/programming-9/shell-script-to-check-root-user-365651/)

kushalkoolwal 09-21-2005 01:13 PM

Shell Script to check root user?
 
Hi,

I am writing a shell script which has various admininstration commands and therefore the script is to be run as a 'root' user. How can I check in the beginning my script whether the user is logged on as a 'root' user or not?

druuna 09-21-2005 01:30 PM

Hi,

One way is to check the UID (which is set by the shell) of the current user (should be 0 for root).

I.e.:
Code:

if [[ $UID == "0" ]]
then
  <root commands>
fi

Hope this helps.

sirclif 09-21-2005 02:32 PM

well, if you only want the root user to be able to run the script, you could just let root own the script and set the permissions so that only root can run it.

If all users are allowed to run the script, but only root users are allowed to access the full functionalitiy through some command line options or something, then you would probably want check inside your script.

kushalkoolwal 09-21-2005 02:51 PM

Thanks it worked!!!

chrism01 09-22-2005 12:15 AM

Or look into sudo ...


All times are GMT -5. The time now is 01:59 PM.