Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 11-29-2008, 03:10 AM   #1
LQ Newbie
Registered: Nov 2008
Posts: 3

Rep: Reputation: 0
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" ]
echo "Cannot proceed with setup.You must be logged in as root!"
while [ $proc != "y" -a $proc != "Y" -a $proc != "n" -a $proc != "N" ]
echo -n "Are you ready to proceed?(Y/N)"
read proc
if [ $proc == "n" -o $proc == "N" ]
if [ $proc == "y" -o $proc == "Y" ]

The error i get:
line:2 [: !=: unary operator expected.
But the script continues to run...
Any help eliminating that error please!
Thanks in advance.
Old 11-29-2008, 05:21 AM   #2
Senior Member
Registered: Mar 2006
Posts: 1,896

Rep: Reputation: 61
Welcome to LQ!

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

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:

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:

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

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

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!

Last edited by blackhole54; 11-29-2008 at 05:25 AM. Reason: wording correction
Old 11-29-2008, 05:48 AM   #3
LQ Newbie
Registered: Nov 2008
Posts: 3

Original Poster
Rep: Reputation: 0
Thumbs up 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
Old 11-29-2008, 01:04 PM   #4
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Unary operator error hinetvenkat Programming 7 10-26-2008 12:22 PM
unary operator expected MONKEYJUDO Linux - Newbie 1 07-13-2008 07:05 AM
build error -- can not find operator new George2 Programming 1 11-05-2007 01:10 PM
Binary operator expected - error mike9287 Linux - Newbie 9 07-17-2006 09:27 AM
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 03:21 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:08 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration