11

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 ?

4m1nh4j1
  • 4,289
  • 16
  • 62
  • 104

4 Answers4

11

You have syntax error in your if condition, use this if condition:

if [ "$EUID" -ne 0 ];

OR using [[ and ]]

if [[ "$EUID" -ne 0 ]];
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 2
    Whats the difference between using `[ ]` and `[[ ]]` ? – Havenard Oct 25 '13 at 19:32
  • `[` is actually an external program as `/bin/[` and `[[ ]]` are new BASH constructs to do test evaluation. – anubhava Oct 25 '13 at 19:35
  • 1
    Using the first, I have this error : [: Illegal number: Using the second, I have this error : [[: not found – 4m1nh4j1 Oct 25 '13 at 19:42
  • Okay, so I suppose `$EUID` is something you fetched using `pidof` right? It so happens `pidof` will return an empty string if it fails to find the process, not 0. Thats why you are having this error. Try `if [ "$EUID" == "" ];` instead. – Havenard Oct 25 '13 at 19:45
  • 4
    @anubhava: `[` is both an external program (also known as `test`) *and* a bash built-in command. If you're seeing `[[: not found`, you're probably not using bash. Is the first line of your script `#!/bin/sh`, `#!/bin/bash`, or something else? – Keith Thompson Oct 25 '13 at 19:45
  • @KeithThompson: Yes agreed `[` is both an external program (also known as test) and a bash built-in command. – anubhava Oct 25 '13 at 19:47
  • @4m1nh4j1: Your question was tagged as `BASH` that's why I suggested use of `[[ ]]` – anubhava Oct 25 '13 at 19:47
  • 1
    @4m1nh4j1: If this answer helped you solve your problem, please consider marking it as "accepted", so users facing a similar problem in the future will be able to spot it easily. – anubhava Oct 25 '13 at 19:53
4

You have syntax error in your if condition, use this if condition:

if [ "$EUID" -ne 0 ];

OR using [[ and ]]

if [[ "$EUID" -ne 0 ]];

If you use the KSH88+/Bash 3+ internal instruction [[, it's not necessary to use doubles quotes around the variables operands :

[ ~/test]$ [[ $var2 = "string with spaces" ]] && echo "OK" || echo "KO" 
OK 

Instead of the external command test or his fork [ :

[ ~/test]$ [ $var2 = "string with spaces" ] && echo "OK" || echo "KO" 
bash: [: too many arguments
KO 
[ ~/test]$ [ "$var2" = "string with spaces" ] && echo "OK" || echo "KO" 
OK 

Of course, you also have to choose the operators according to the type of operands :

[ ~/test]$ var1="01" 
[ ~/test]$ [ "$var1" = "1" ] && echo "OK" || echo "KO" 
KO 
[ ~/test]$ [ "$var1" -eq "1" ] && echo "OK" || echo "KO" 
OK 
Idriss Neumann
  • 3,760
  • 2
  • 23
  • 32
4

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:

if [ $(id -u) -ne 0 ]; then echo "ur not root bro"; exit 1; fi

would be a good way to do it.

joshmeranda
  • 3,001
  • 2
  • 10
  • 24
jrrs
  • 98
  • 4
1

using

sudo bash shell_script.sh

instead of

sudo sh shell_script.sh

solved in my case.

Eswar Chitirala
  • 486
  • 6
  • 14