Register
It is currently Wed Dec 13, 2017 10:25 pm

running a script as root in the script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 4 posts ] 
Author Message
 PostPosted: Wed Nov 15, 2017 5:20 pm   

Joined: Tue Apr 12, 2016 4:11 am
Posts: 51
hi all,

i think i have got the solution for this but want to run it past you guys first

when i run a script sometimes its necassary to sudo to root so it can create users, chmod etc etc, the normal way for me doing this is just to simply run the script as root

but i have created a user and that user can sudo up to root level so im thinking of doing the "if/then" statement with the "id" command

Code:
if [ id -u root -ne 0 ]; then
su
echo "type in the root password"

rest of the commands go here

is this a good way?

thanks,

rob


Top
 Profile  
 PostPosted: Thu Nov 16, 2017 4:46 am   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
Seems OK to me, if closing else/fi follows.

Do you really need this?
Why not just start the script as root?


Top
 Profile  
 PostPosted: Thu Nov 16, 2017 8:33 am   

Joined: Tue Apr 12, 2016 4:11 am
Posts: 51
should had made it -

Code:
if [ id root -ne 0 ]; then
echo "type in the root password"
read pwd
su $pwd 
fi


yeah i do need it as i dont want to give out the root password, i would rather make a user account and get that user to sudo up to root level


Top
 Profile  
 PostPosted: Thu Nov 16, 2017 3:04 pm   

Joined: Mon Oct 20, 2014 9:53 am
Posts: 560
Code:
su $PWD

is terrible wrong.

The su command means SubstituteUsercontext and changes the environment and all rights to the one's the given user got. If no user given, it defaults to root.
I think you wanted sudo ( SubstituteUsercontextAndDO).

$pwd ist a kind of misleading.
pwd is a command ( PrintWorkingDirectory) not a command or password.


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 19 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP