1

Why does this script give error?

#!/bin/bash
# This script checks whether 
# the current user is root or 
# not.
if [ $UID -ne 0 ] then
    echo "Non-root user."
else
    echo "Root user."
Fi

Output

myuser@kali:~$ ./user.sh
./user.sh: line 7: syntax error near unexpected token `else'
./user.sh: line 7: `else'
myuser@kali:~$
0

1 Answer 1

5

You have forgot to put ; between if and then:

if [ "$UID" -ne 0 ]; then
    echo "Non-root user."
else
    echo "Root user."
fi

Also the if conditional construct ends with fi, not Fi.

; is basically a shorthand for newline. If you want you can use then in the next line to avoid ;:

if [ "$UID" -ne 0 ]
then
    echo "Non-root user."
else
    echo "Root user."
fi
2
  • let me see again. Commented Jun 18, 2015 at 13:58
  • 1
    Alternatively you may put the then keyword on a seperate line (the line below if) to avoid the need of the semicolon ;. Commented Jun 18, 2015 at 14:00