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:
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!