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. |
Welcome to LQ!
I believe the following line is the one giving you a problem Code:
if [ $USERNAME != "root" ] 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" ] Code:
if [ "$USERNAME" != "root" ] 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 ] 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! :) |
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 |
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. |