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
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.