balug-shell_programming_finding_out_if_you_are_root

This is part of The Pile, a partial archive of some open source mailing lists and newsgroups.



To: balug-talk@balug.org
Subject: Re: [Balug-talk] simple c shell programming question 

xli987@yahoo.com.cn wrote:

> I am new to C shell programming, have some simple
> quesitions, appreciate your help.
> 
> 1) How to I make sure that the shell program I create
> must be executed in super user mode ?
> 
> 2) I am executing command in the scripts and it might
> take a while for it to complete, ( like installing a
> driver ), how do I wait and check for godo return
> status before moving to the next ?

Okay, my guess is that what you're doing here is writing
some kind of installation script for your software.  Is 
that correct?  

So, if I understand your first question, you may be looking
for a trick like this:

 === cut === 

#!/bin/sh
# dont_run_if_not_root - doom@kzsu.stanford.edu
#                    Tue Oct  1 20:17:59 2002

if [ `whoami` != 'root' ]; then
   echo "Sorry, you must have superuser privileges to run this"
   exit
fi
echo  "Proceeding with installation" 

 === cut ===

If you want something portable across a wider variety of
unixes, you may not be able to rely on "whoami" being
present.  Cross-platform unix shell scripting can be a nasty
subject...

The following is one solution that would probably work:

 === cut === 

#!/bin/sh
# dont_run_if_not_root_portable - doom@kzsu.stanford.edu
#                    Tue Oct  1 20:17:59 2002

# (Note: I am going to town on paranoia here to make sure the 
# temp file name you use is not already in use for some
# other purpose.)

BASENAME=watley
TEMPFILENAME="/tmp/${BASENAME}.$$"
while [ -e ${TEMPFILENAME} ]; 
do
   BASENAME="${BASENAME}_yogsoththoth"
   TEMPFILENAME="/tmp/${BASENAME}.$$"
done

# Here's the real trick: create the temp file and look 
# at the directory listing to see who created it.

touch ${TEMPFILENAME}
THOUART=`ls -la ${TEMPFILENAME} | awk '{print $3}'`
rm ${TEMPFILENAME}

if [ ${THOUART} != 'root' ]; then
   echo "Sorry, you must have superuser privileges to run this"
   exit
fi
echo  "Proceeding with installation" 

 === cut === 

By the way, one problem you need to worry about is that
while I'm trying to run the original bourne shell (sh) in
these scripts, because it's pretty universal, on linux (or
at least, on RedHat linux) you don't really have 'sh': 
instead /bin/sh is a symlink to /bin/bash.  While the GNU
"Bourne again shell" is backwardly compatible to the
original, this creates a trap for people who might want to
write a shell script to run on some other system.  You could
accidentally start using some bash-specific feature that 
won't run everywhere. 


Other tricks you could play with include checking the HOME
environment variable, testing to see if it's './root' (note,
stuff like USER isn't any good, since "su" leaves that
alone).

It might be best to do every test that you can think of, 
and run the script if *any* of them turn up "root".

===

the rest of The Pile (a partial mailing list archive)

doom@kzsu.stanford.edu