1

I want to check if the user running the script is root or not before running. But it doesn't seem to work. I'm currently logged in as root, but its something wrong with the syntax I think.

if[[ $EUID -ne 0 ]];
then
 echo "Sorry, you are not root."
 exit 1
else
 echo "You are root, script will continue."
fi
1
  • You have a spacebar bug, see @user3553031's answer Commented Apr 23, 2014 at 23:08

2 Answers 2

4

You need a space between if and [.

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

Comments

1

Why not simething like:

usrname=$(whoami)
if [ $usrname = "root" ]
echo "Sorry, you are not root."
 exit 1
else
  echo "You are root, script will continue."
fi

Although I think the problem is merely your syntax with the if statement:

  • Why double brackets
  • Why a ; after the if?
  • A space between if and [, thus if [

bash is very sensitive to syntax. For instance spaces between the assignment operator (=) are not allowed either.

1 Comment

When I find that user is not my desired user, instead of exit, how can I run the rest of the script as my desired user? Please look at stackoverflow.com/questions/67646925/…

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.