Domanda

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
È stato utile?

Soluzione

You need a space between if and [.

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top