ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I occasionally put together shell scripts to automate tasks, which would otherwise need me to remember very long commands (e.g. converting digital camera movie clips into DivXs for our Web site). I'm not really a programmer, but can usually find enough advice via Google to get by.
One small script I'm trying to get working, assigns a disk image file (which I use for running FreeDOS under Bochs) to the loopback device and then mounts it, so I can copy files to and from the image.
Because the "mount" command is involved, I want the script to check whether the user is logged in as superuser, and at the very least, exit if not. I found this routine and tried it as a non-superuser:
Code:
if [ $UID -ne $ROOT_UID ]
then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
It gave me this message:
Code:
[: 500: unary operator expected
...and didn't exit, so the other two operations (loopback, and mount) failed as I was not the superuser.
Does anyone know what might be wrong with the above code? Alternatively, how could the script be modified so that "sudo" would be called if the user was not root? (I would be the only user who could execute it.)
What your missing is quotes around the variables try:
Code:
ROOT_UID=0 # Only users with $UID 0 have root privileges.
E_NOTROOT=67 # Non-root exit error.
if [ "$UID" -ne "$ROOT_UID" ]
then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
To run sudo just call sudo from the script and check its output for success. The Advanced Bash-Scripting Guide has the details of how to do this.
In the case where you want to check if invoking user has invoked script via sudo (and therefore has root privileges), the 'id' command will give you the effective user and group id.
UID is a preset bash shell variable, but ROOT_UID is not. Unless you've defined it previously in the script, it will be null, breaking the test. Since the root uid is almost always "0", is there any reason not to use that instead?
Also, numeric comparisons like this are better done with ((..)), and string and complex comparisons with [[..]]. The old [ command has some weaknesses, and is really only necessary when writing portable, posix-compliant scripts.
Code:
if (( UID != 0 )); then
#or this evaluates the same:
if (( UID )); then
"0" in an arithmetic test evaluates to false, and all other values will be true. So any user other than root will cause the subcommands to execute.
I really dislike using 'if's, not that they are wrong, but I find that the exit code is usually enough.
For example, If you are not concerned about using Bashisms
Code:
(( `id -u` )) && echo "Must be root" && exit 1
## Bash's built-in (('s return true for any non zero value.
## If your running as root, `id -u` would be 0
Personally I also like most of my scripts to run with dash, unless I have reason to explicitly call bash.
For example, I prefer this POSIX equivalent.
Code:
[ `id -u` -ne 0 ] && echo "Must be root" && exit 1
## Some call this ugly, I actually find this cleaner to read.
## You could always replace '['s with the verbose version 'test'
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.