static Posted June 27, 2003 Report Share Posted June 27, 2003 Hey - I read somewhere that you can do this: su -c "guarddog" and guarddog will be run as root. Assuming I remembered that correctly, is it possible to have su -c "sh myscript.sh" to have all the commands in the script executed as root? TIA Quote Link to comment Share on other sites More sharing options...
tyme Posted June 27, 2003 Report Share Posted June 27, 2003 I believe it should, as (if memory serves) inheritance exists in shell scripts (that is, a shell script passes on it's properties to processes it starts) Quote Link to comment Share on other sites More sharing options...
Gowator Posted June 27, 2003 Report Share Posted June 27, 2003 Worth remembering is that <myscript> will pass its inheritance to anything else it starts. So if the scruipt is owned by (say apache) and someone added a line the script would also pass root to the line ... It's like setting a suid bit. Or running a rsh ... Not actually important (in this case) , just a security comment... Quote Link to comment Share on other sites More sharing options...
static Posted June 27, 2003 Author Report Share Posted June 27, 2003 OK then New (related) question: How can I make a "function" in the script so that I can run sh myscript.sh as user and it'll mosey along until it calles a different part of the script meant for root, then whether or not it was successful do something else (and you'd know if it was successful based on what it returned, hence why I said function and not procedure) For Example, an install script that would look like this Pseudo!! mkdir -a ~/.q3a/baseq3 #was is -a to create the necessary parent dirs? cp /mnt/cdrom/games/quake3/*.cfg ~/.q3a/baseq3 if [[ `call rootstuff` ]] is successful; then echo Everything Worked! Press ESC to exit or any other key to play! if ESC pressed exit else quake3 #rootstuff: #as root do this mkdir -a /usr/local/games/quake3/baseq3 #was it a ?? sh /mnt/cdrom/games/linuxq3a-1.32.x86.run ln -s /the_void/games/Quake III Arena/baseq3/* /usr/local/games/quake3/baseq3/ return whether or not successful Quote Link to comment Share on other sites More sharing options...
aru Posted June 27, 2003 Report Share Posted June 27, 2003 Not tested, but probably this should be OK: rootstuff () { mkdir -a /usr/local/games/quake3/baseq3 && sh /mnt/cdrom/games/linuxq3a-1.32.x86.run && ln -s /the_void/games/Quake III Arena/baseq3/* /usr/local/games/quake3/baseq3/ || return 1 # probably this part won't be necessary since if one of the above # commands breaks the return status will be >0 } if su -c rootstuff #here you'll have to enter root's passwd then echo Everything Worked! Press ESC to exit or any other key to play! ... fi Quote Link to comment Share on other sites More sharing options...
static Posted June 27, 2003 Author Report Share Posted June 27, 2003 wow! and that works in bash programming, eh? Coooool! 8) Going to have to try this when I get home! Quote Link to comment Share on other sites More sharing options...
aru Posted June 27, 2003 Report Share Posted June 27, 2003 My example won't work. I wrote it w/o thinking too much. I guess that I was expecting that "su -c rootstuff" would have expanded the function before starting the subshell <--- stupid of me. Obviously that is not true since su will first start a root shell and then it will execute the commands. This other example works fine: rootstuff () { su -c ' mkdir -a /usr/local/games/quake3/baseq3 && sh /mnt/cdrom/games/linuxq3a-1.32.x86.run && ln -s /the_void/games/Quake III Arena/baseq3/* /usr/local/games/quake3/baseq3/ ' } if rootstuff then echo 'Everything Worked! Press ESC to exit or any other key to play!' ... else echo 'Something went wrong!!!' fi Quote Link to comment Share on other sites More sharing options...
sarah31 Posted June 28, 2003 Report Share Posted June 28, 2003 there's always sudo. Quote Link to comment Share on other sites More sharing options...
aru Posted June 28, 2003 Report Share Posted June 28, 2003 But static's stuff looks more an installer than anything Quote Link to comment Share on other sites More sharing options...
static Posted June 30, 2003 Author Report Share Posted June 30, 2003 Thanks! My network admin skills expanded, I am now closer than ever to taking the world!! THE WORLD!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.