[CLUE-Tech] shell programming

David Guntner davidg at akaMail.com
Wed Dec 11 13:12:16 MST 2002


Dave Price grabbed a keyboard and wrote:
>
> On Wed, Dec 11, 2002 at 03:13:16AM -0800, David Guntner wrote:
> > 
> > #!/bin/bash
> > 
> > if [ $LOGNAME = "root" ]; then
> >   echo "Hello root"
> > else
> >   echo "You aren't root"
> > fi
> > 
> > 
> > Will do what you want.
> 
> Not with my bash ...  $LOGNAME does not change when you `su
> 
> [davep at ldpx:~/images]$ su
> [root at ldpx images]# echo $LOGNAME
> davep
> 
> [root at ldpx images]# echo $UID
> 0
> 
> ... but $UID does ...

Try doing "su -" instead.  Including the "-" tells it to clear your 
environment and set it up as though root had logged in directly.  If you 
just "su" (without the "-"), the current enviroment variables that are 
currently present for the user ID that you're logged in as remain in place. 
That's why it's a good idea to always use the "-" when trying to do things 
as another user.  This works for when you're trying to run a script when 
logged in as root that needs to be done as another user as well.  Thus, if 
you wanted to run something as the "news" user while you were root, you 
should start it as:

su - news -c {the script to run}

This makes sure that the script doesn't inherit any of your exported 
variables from your shell when it runs.

                --Dave
-- 
      David Guntner      GEnie: Just say NO!
 http://www.akaMail.com/pgpkey/davidg or key server
                 for PGP Public key




More information about the clue-tech mailing list