Click to See Complete Forum and Search --> : small script problem


Jods
06-11-2001, 08:55 PM
this is a script to be run be su, problem is after su has filled in the password correctly the script "hangs" instead of continuing the rest of the script. If the script could be run all over again the problem would be solved...
well, I suppose.
thanx

#!/bin/bash
S1='$LOGNAME'
S2='root'
if [ $S1!=$S2 ];
then
echo "Sorry, your logname is $LOGNAME, not root"
echo "You must have root or su access to run this script"
su
rv='$?'
if [ $rv=1 ]
then echo canceled
exit
else break
fi
else break
fi
echo "The End"
:confused:

dchidelf
06-11-2001, 11:27 PM
If I understand you want a script that, if a user does not have root privlegdes, will prompt for a password and su them to root if they know it....

LOGNAME doesn't necessarily change to root after a user su's to root... so it may be more appropriate to use either $EUID to find the users effective userid... or whoami for the name...

#!/bin/bash

MYUID=`whoami` # get the users name

if [ $MYUID != "root" ]; then

echo "You must have root access to run this script"
su root -c "/foo/bar/scriptname"
# switch to root and run the script
exit

fi

# the rest of the script
# only root will be here

echo $MYUID $EUID
echo "DONE"


This should work I guess...

Chad

AndySax
06-13-2001, 11:05 AM
Firstly, variable interpolation will not take place if you use single quotes. echo '$LOGNAME' will print $LOGNAME instead of the value stored in $LOGNAME. You need to use double quotes (").

-AS