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

3
  • The second argument to grep is supposed to be a file. The file named "1" does not exist in the current directory. Commented Feb 6, 2015 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). Commented Feb 6, 2015 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? Commented Feb 6, 2015 at 21:03

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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
2

In bash, you can just test $UID:

if ((UID==0)); then
   # I'm root
fi

Comments

1

Try whoami:

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

1 Comment

what is test in the above?
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'

2 Comments

shouldn't whomai be in back tics?
@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.
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.

Comments

0

Use whoami:

if [ `whoami` == "root" ] ; then
    echo "root"
fi

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.