Du könntest versuchen eine Loginshell in deinem Script zu starten:
man bash
/bin/bash -l
oder
/bin/bash --login
-l Make bash act as if it had been invoked as a login shell (see INVOCATION
below).
--login
Equivalent to -l.
Ich sehe aber darin keinen Sinn.
Die Programme werden immer mit den Rechten der jeweiligen User ausgeführt von denen sie gestartet wurden. Die Rechte werden sozusagen vererbt. Also wenn du dich in der Konsole einloggst und das jeweilige Shellscript startest, dann wird dieses Shellscript mit deinen Rechten gestartet. Das hängt alles mit dem Unix-Systemaufruf fork() und dem Systemaufruf execl() von Unix zusammen, mit denen ein neuer Prozess gestartet wird.
Du kannst aber die USER ID im Shellscript mit einer if Anweisung überprüfen und daraufhin im Programm verzweigen.
In etwa so:
if [ `$ID -u` != 0 ] ; then
echo
echo "Dieses Shellscript braucht root-Rechte !"
echo
exit 1
fi
root hat die ID 0 (NULL) . Die anderen IDs kannst du aus der Datei /etc/group herausnehmen.
Oder ein Beispiel aus dem Advanced Bash-Scripting Guide
http://www.tldp.org/LDP/abs/html/internalvariables.html#AMIROOT
Im Advanced Bash-Scripting Guide habe ich diesen Hinweis gefunden:
http://www.tldp.org/LDP/abs/html/restricted-sh.html
man bash
.....
-r If the -r option is present, the shell becomes restricted (see RESTRICTED
SHELL below).
RESTRICTED SHELL
If bash is started with the name rbash, or the -r option is supplied at invocation,
the shell becomes restricted. A restricted shell is used to set up an environment
more controlled than the standard shell. It behaves identically to bash with the
exception that the following are disallowed or not performed:
· changing directories with cd
· setting or unsetting the values of SHELL, PATH, ENV, or BASH_ENV
· specifying command names containing /
· specifying a file name containing a / as an argument to the . builtin command
· Specifying a filename containing a slash as an argument to the -p option to
the hash builtin command
· importing function definitions from the shell environment at startup
· parsing the value of SHELLOPTS from the shell environment at startup
· redirecting output using the >, >|, , >&, &>, and >> redirection operators
· using the exec builtin command to replace the shell with another command
· adding or deleting builtin commands with the -f and -d options to the enable
builtin command
· Using the enable builtin command to enable disabled shell builtins
· specifying the -p option to the command builtin command
· turning off restricted mode with set +r or set +o restricted.
These restrictions are enforced after any startup files are read.
When a command that is found to be a shell script is executed (see COMMAND EXECUTION
above), rbash turns off any restrictions in the shell spawned to execute the script.
......
PS: Kauf dir mal ein kleines Buch für die Shellprogrammierung:
Shell-Programmierung für Unix und Linux (Taschenbuch)
von Rainer Krienke (Autor)
http://www.amazon.de/Shell-Programmierung-Unix-Linux-Rainer-Krienke/dp/3446217223/ref=sr_1_1?ie=UTF8&s=books&qid=1214328290&sr=8-1
Oder wenn es ausführlich sein soll:
Shell-Programmierung: Das umfassende Handbuch (Gebundene Ausgabe)
von Jürgen Wolf (Autor)
http://www.amazon.de/Shell-Programmierung-umfassende-Handbuch-J%C3%BCrgen-Wolf/dp/3836211572/ref=sr_1_1?ie=UTF8&s=books&qid=1214328366&sr=1-1