Checking if you're running as root
There are different ways of checking if we're running a script as root. We can use environment variables, just as we can use the whoami
or id
commands to check if it equals root/number 0
or not.
Getting ready
We'll continue using the cli1
machine for this recipe, so make sure that it's powered on.
How to do it…
Let's create a short snippet of Bash shell script code that's going to help us find out whether we're running a script as root or not. It's a rather simple thing to do in Linux, considering that we have easy access to an environment variable called EUID
, and reading its value is enough to determine whether we're running as root (EUID=0) or not (EUID value > 1):
#!/bin/bash # V1.0 / Jasmin Redzepagic / 01/11/2021 Initial script version # Distribution allowed under GNU Licence V2.0 # First, we need to check if our environment variable UID is set to # 0 or not and branch...