View Single Post
Posts: 2,152 | Thanked: 1,490 times | Joined on Jan 2006 @ Czech Republic
#5
Originally Posted by darkclark View Post
I was wondering if it was possible to externalize the header to gain root access and then include it somehow, or better create a standalone script that establishes root access and exec that at the start of any script needing root acccess...but seeing as this gains root first, then relaunches itself....that may not be possible because of the recursive nature.?
Well it is not that long so easiest is just to include it as header. If the script does not need keyboard input, there is shorter version
Code:
#!/bin/sh
# use gainroot to become root and relaunch itself
if [ `id -u` != 0 ] ; then
#if not already root, call itself as root
exec sudo gainroot <<EOF
exec $0 $*
EOF
exit $?
fi
# real script follows
And you can strip comments too to save few bytes or put it to one line to save screen space. And also remove one paranoic exit call
Code:
#!/bin/sh
if [ `id -u` != 0 ] ; then exec sudo gainroot <<E exec $0 $* E ; fi
or even

Code:
#!/bin/sh
[ `id -u` != 0 ] && exec sudo gainroot <<E exec $0 $* E
Still worth of having it in separate file?


Also please note that this is just a hack that (ab)uses gainroot script, correct way of calling something as root is to use sudo and add your scripts to /etc/sudoers (beware of messing up this file or change permissions, reboot loop needing reflash may be the result)
__________________
Newbies click here before posting. Thanks.

If you really need to PM me with troubleshooting question please consider posting it to the forum instead. It is OK to PM me a link to such post then. Thank you.

Last edited by fanoush; 2007-08-17 at 07:04. Reason: paranoid exit call not exec
 

The Following User Says Thank You to fanoush For This Useful Post: