- Linux Shell Scripting Cookbook(Third Edition)
- Clif Flynt Sarath Lakshman Shantanu Tushar
- 112字
- 2021-07-09 19:46:00
Checking for super user
The UID environment variable holds the User ID. Use this value to check whether the current script is being run as a root user or regular user. Consider this example:
If [ $UID -ne 0 ]; then echo Non root user. Please run as root. else echo Root user fi
Note that [ is actually a command and must be separated from the rest of the string with spaces. We can also write the preceding script as follows:
if test $UID -ne 0:1 then echo Non root user. Please run as root else echo Root User fi
The UID value for the root user is 0.