0

Here is a simple function that our script uses to find if user is logged in as root.

do_check_user(){
    id | grep root 1>/dev/null 2>&1
    if test "$?" != "0"
        then
            echo ""
            echo " Invalid login, Please logon as root and try again!"
            exit 0
    fi
}

I do not completely understand how this function works. I tried some searching online to find bits and parts of how it is implemented. but it is still not clear to me.

Especially these lines:

id | grep root 1>/dev/null 2>&1
    if test "$?" != "0"

I tried doing step by step. But I get error

id | grep root 1
grep: 1: No such file or directory

if you could explain me this syntax and what it is doing, it will be very helpful

Thanks

brain storm
  • 30,124
  • 69
  • 225
  • 393
  • The second argument to grep is supposed to be a file. The file named "1" does not exist in the current directory. – fork0 Feb 06 '15 at 20:04
  • The line with grep contains output redirection operators (`1>/dev/null` and `2>&1`), and is supposed both stderr (standard error output channel, `2>`) and stdout (standard output channel, `1>`, usually written just `>`) into /dev/null (aka, nowhere). – fork0 Feb 06 '15 at 20:07
  • That's a terrible way of doing the test, by the way. Suppose you had a user named `grooten` or even a group named `arrowroot_biscuits`? – rici Feb 06 '15 at 21:03

6 Answers6

2
  1. id - print real and effective user and group IDs

  2. grep searches through the output of id for root

  3. 1>/dev/null 2>&1 sends stdout/stderror to /dev/null ; therefore you won't see the output 1>/dev/null sends only stdout to /dev/null

  4. if test "$?" != "0" checks the status of the last executed command which is grep, if it is 0 means sucessful and if it is not 0, you will get the messages.

Aman
  • 1,157
  • 7
  • 13
  • thanks for elaborate answer. In point 3 above, is stdout or stderr from grep send to `/dev/null`. and what does final `2>&1` mean? – brain storm Feb 06 '15 at 20:19
  • 1 is stdout. 2 is stderr. `2>&1` redirects stderr to stdout and by sending `1>\dev\null`, both of them will be send to `\dev\null`; [Cool](http://stackoverflow.com/questions/818255/in-the-shell-what-is-21) – Aman Feb 06 '15 at 20:24
2

In bash, you can just test $UID:

if ((UID==0)); then
   # I'm root
fi
rici
  • 234,347
  • 28
  • 237
  • 341
1

You simply can use whoami command.

#!/bin/bashyou
if [[ $(whoami) == 'root' ]] ; then 
  echo "it's root"
fi

EDIT Only for bash: you can also you $EUID variable which refers to user identifier, so in root case it equals 0

(($EUID == 0)) && echo 'root'
deathangel908
  • 8,601
  • 8
  • 47
  • 81
  • 1
    shouldn't whomai be in back tics? – EJK Feb 06 '15 at 20:06
  • @EJK, This answer, at this time, has not been edited and you're confusing it with a similar answer which is employing the legacy use of backticks over `$(..)`, which some consider the latter to be the current more preferred method. – user3439894 Feb 06 '15 at 20:20
1

Try whoami:

do_check_user() { test $(whoami) = root; }

fork0
  • 3,401
  • 23
  • 23
1

I see a lot of people here are using operands == and != to compare true and false bits, I would suggest using nothing for the == to represent true and just using ! for false.

EG

if ((UID)); then echo 'This means the test is Boolean TRUE and the user is not root'
if ((!UID)); then echo 'This means the test is Boolean FALSE and the user is root'

Or if comparing a numerical variable to Boolean TRUE or FALSE

if [[$a]]; then echo "If the variable 'a' is a 1 then it's Boolean TRUE"
if [[!$a]]; then echo "If the variable 'a' is a 0 (zero) then it's Boolean FALSE"

When comparing TRUE or FALSE the operations == and != is not needed, this saves a few key strokes too.

0

Use whoami:

if [ `whoami` == "root" ] ; then
    echo "root"
fi
Bohemian
  • 412,405
  • 93
  • 575
  • 722