How to execute a script as super user first checking the user and getting pass from askpass if not super user
I just call sudo if the program needs root permissions, but doesn't have:
#!/bin/bash
if [ $(id -u) != 0 ]; then
echo "This script requires root permissions"
sudo "$0" "$@"
exit
fi
"$0"
contains the name of the script, "$@"
optional arguments. It may be omitted if your program does not accept arguments.
Note: this shellscript is expected to be run in a shell, if this script should run as GUI, use something like gksu
or kdesudo
instead of sudo
.
Related videos on Youtube
pahnin
Updated on September 17, 2022Comments
-
pahnin over 1 year
thers a similar question out there How can I determine whether a shell-script runs as root or not?
I have the same doubt with different result
Is it possible to, within the BASH script prior to everything being run, check if the script is being run as superuser, and if not, print a message saying You must be superuser to use this script, then subsequently
get pass from the user using askpass or something like that then execute the same script as superuser?
-
Thomas Ward about 13 yearsPotential duplicate of my question, no?
-
Lekensteyn about 13 years@EvilPhoenix: the question is not "how to detect that I am root" but "how can I make a script root through a mechanism like sudo".
-
Michael Mrozek over 12 years
-