LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 11-29-2008, 02:10 AM   #1
Lynda_M
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" ]
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.
 
Old 11-29-2008, 04:21 AM   #2
blackhole54
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


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!

Last edited by blackhole54; 11-29-2008 at 04:25 AM. Reason: wording correction
 
Old 11-29-2008, 04:48 AM   #3
Lynda_M
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, 12:04 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

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


Reply


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 11:22 AM
unary operator expected MONKEYJUDO Linux - Newbie 1 07-13-2008 06:05 AM
build error -- can not find operator new George2 Programming 1 11-05-2007 12:10 PM
Binary operator expected - error mike9287 Linux - Newbie 9 07-17-2006 08:27 AM
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 02:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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