Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I change from regular "user" to "root" ID within the

Status
Not open for further replies.

hd7106

Programmer
Nov 27, 2002
21
US
I am trying to run a specific script that allows me to do some file-maintenance. The script runs error-free and does what it is supposed to do. In fact I am supposed run this script (Bourne shell) as a normal user and not as "root". However, at some point in the script one of the binaries extracts the files which I need to rename later in this shell script.

This binary extracts these files as user "root" and group "other". When I try to executre "mv" comnmand to rename these files it would obviously fail to execute as it is run by a normal user. If I switch to "root" user I can rename the file through this same shell script. (Commenting out the lines previous for testing).

I need to be able to run this application using normal user ID and because of this binary, I need to switch to "root" user at the end, so that I can rename these large number of files.

How can I switch to root ID within the script and be able to run this command?

e.g.
#!/bin/sh
< normal user script flow>
....
.....
.....
.....
< portions of the script requires &quot;root&quot; user ID>
......
......
......
<switch it back to normal user....>

Will appreciate any help...
 
If the user is root the script below simple runs but if the user is not root an su command is issued and the script prompts for the root password before calling itself again. I'm sure you'll be able to hack this method to work for your problem.

# Check the syntax I'm working from memory.....

#!/bin/sh
# If I'm not root; login and run script
# otherwise just run the script
if ( `uid` != 0 )
su - root -c ${0} # call myself
# The password prompt appears here
else
....
....
fi

hope this helps

 
sudo can do this. you have to edit the /etc/sudoers to enable the normal user execute the root command.

if you use 'su' only, then you have to install a 'expect' tool which enable your shell scripts to interactive with your system , then it can enter the password automactically when a password prompt appears.

good luck.

There are only 10 types of people in the world: Those who understand binary, and those who don't
 
Thanks for the tips, and very much appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top