Frage

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
War es hilfreich?

Lösung

You need a space between if and [.

Andere Tipps

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top