When I run this bash script :
if [ [$EUID -ne 0] ]; then
echo "This script must be run as root" 1>&2
exit 1
else
printf " whathever "
exit 0
fi
I have this error :
./myScript: 15: [: Illegal number: [
Do you see any problem ?
When I run this bash script :
if [ [$EUID -ne 0] ]; then
echo "This script must be run as root" 1>&2
exit 1
else
printf " whathever "
exit 0
fi
I have this error :
./myScript: 15: [: Illegal number: [
Do you see any problem ?
You have syntax error in your if condition, use this if condition:
OR using
[[
and]]
two suggestions apart from what everyone else has pointed out already.
1) rather than doing 'else [bunch of code because we are root] fi', just replace the 'else' with 'fi'. once you've tested for the failure condition you are concerned about and taken appropriate action, no need to continue to be within the body of the conditional.
2) $EUID is a bashism, if you would like to make this portable to shells such as ksh, replacing it with:
would be a good way to do it.
If you use the KSH88+/Bash 3+ internal instruction
[[
, it's not necessary to use doubles quotes around the variables operands :Instead of the external command
test
or his fork[
:Of course, you also have to choose the operators according to the type of operands :