LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   unary operator expected error! (https://www.linuxquestions.org/questions/programming-9/unary-operator-expected-error-687011/)

Lynda_M 11-29-2008 02:10 AM

unary operator expected error!
 
Hi everyone,
I'm a Linux newbie and would appreciate any help solving my issue...
I'm running a script to set up Linux OS environment, the script works great but i get an error on the following portion:

if [ $USERNAME != "root" ]
then
echo "Cannot proceed with setup.You must be logged in as root!"
exit
else
proc=m
while [ $proc != "y" -a $proc != "Y" -a $proc != "n" -a $proc != "N" ]
do
echo -n "Are you ready to proceed?(Y/N)"
read proc
if [ $proc == "n" -o $proc == "N" ]
then
exit
else
if [ $proc == "y" -o $proc == "Y" ]
then
continue
fi
fi
done
fi

The error i get:
line:2 [: !=: unary operator expected.
But the script continues to run...
Any help eliminating that error please!
Thanks in advance.

blackhole54 11-29-2008 04:21 AM

Welcome to LQ!

I believe the following line is the one giving you a problem


Code:

if [ $USERNAME != "root" ]
There are a couple of points that might be made here.

I suspect $USERNAME is undefined. If that is the case, then what the shell sees (after it attempts to expand $USERNAME -- which expands to an empty string!) is:

Code:

if [ != "root" ]
which, of course, gives an error. One way (but not the only way) to prevent this kind of error when the variable might be undefined or defined to the emptry string is to (double) quote it:


Code:

if [ "$USERNAME" != "root" ]
In this case if the variable is undefined the shell will see:

Code:

if [ "" != "root" ]

which will not generate an error, but the test will fail.


The other point is your choice of a variable. TMK, $USERNAME is not automatically defined. On the Linux systems I have handy $USER is defined. But from the bash man page I don't think it is something that is defined by bash. So I don't know if you can count on it being defined on all distros. (If you want to do a quick check to see if a variable is defined by either bash or your system, you can do a quick check in an interactive shell by typing echo $VARIABLE. If it is defined to something printable you will see its value printed. If you see nothing, it probably isn't defined.)

So you might try and see if the variable $USER works for you. There is another option, which is what I usually use, and that is to use the command id. With the options -u and -n it will print the user's name. So

Code:

if [ $(id -un) != root ]
will run the test that you want (note that the quotes around root are not necessary in bash -- unlike in C). If this is jumping too far ahead of where you are, just ignore my comments about the id command.


Sorry about the longwinded post. But in addition to (hopefully) solving your problem, I was trying to convey some info that I hope to be useful to you going forward.

Happy computing! :)

Lynda_M 11-29-2008 04:48 AM

Problem solved!
 
Thank u so much for ur reply ..Problem solved... :)
I replaced $USERNAME with $(id -un) and it WORKED!!
P.S. ur "longwinded post" was a BIG HELP :D

Tinkster 11-29-2008 12:04 PM

Moved: This thread is more suitable in <PROGRAMMING> and has been moved accordingly to help your thread/question get the exposure it deserves.


All times are GMT -5. The time now is 10:28 AM.