You need a space between if
and [
.
Checking if root inside bash script
Question
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
La solution
Autres conseils
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 theif
? - A space between
if
and[
, thusif [
bash is very sensitive to syntax. For instance spaces between the assignment operator (=
) are not allowed either.
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow