Binary operator expected

Hi Team,

I just started to learn shell scripting and i got this script from an online book and tried to run in my terminal. But it throws error message.

echo $0
-bash

echo $UID
501

cat check_rootuser.sh
#!/bin/bash
# Run as root, of course.
LOG_DIR=/var/log
ROOT_UID=0 # Only users with $UID 0 have root privileges.
LINES=20 # Default number of lines saved.
E_XCD=66 # Can't change directory?
E_NOTROOT=67 # Nonroot exit error.

if [ "$UID" ne "$ROOT_UID" ]
then
echo "Must be root to run this script"
exit $E_NOTROOT
fi
./check_rootuser.sh
./check_rootuser.sh: line 10: [: ne: binary operator expected

please help me to solve this error.

Thanks
Selva

Please share your bash version.

While the construct works well with GNU bash, version 4.1.11(1)-release (amd64-portbld-freebsd9.0) :

if [ "$UID" -ne "$ROOTUID" ]; then echo "Must be root to run this script"; fi
Must be root to run this script

, it doesn't with GNU bash, version 4.3.46(1)-release (x86_64-pc-linux-gnu) :

if [ "$UID" ne "$ROOTUID" ]; then echo "Must be root to run this script"; fi
bash: [: ne: binary operator expected

Looks like a flaw in that bash version...

Hi,

One thing you could try is using != instead of -ne . As RudiC has said, different versions of Bash can behave differently in this type of situation, depending on what they think they're comparing and what the situation precisely is.

bash --version
GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

it looks like, it won't work in GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)

Hi.

When I looked at your script with cat -A , the if showed up as:

if [ "$UID" ?ne "$ROOT_UID" ]$

which suggests that the minus sign is not the ASCII code 45 2D -

Try changing that to a minus sign (on my keyboards, the key to the right of 0 [zero] ).

Did you edit this on Windows or some other non-Linux place?

Best wishes ... cheers, drl