I don't have many scripts that elevate from user privileges to sudo
(super user) powers. Ironically due to the fact your question is about gedit
one of the scripts I have is called sgedit
. It was created because gksu gedit
is no longer supported and because root user can't set tab settings, font preferences, etc.
#!/bin/bash
# NAME: sgedit
# PATH: /mnt/e/bin
# DESC: Run gedit as sudo using $USER preferences
# DATE: June 17, 2018.
# Must not prefix with sudo when calling script
if [[ $(id -u) == 0 ]]; then
zenity --error --text "You cannot call this script using sudo. Aborting."
exit 99
fi
# Get user preferences before elevating to sudo
gsettings list-recursively | grep -i gedit | grep -v history | \
grep -v docinfo | \
grep -v virtual-root | grep -v state.window > /tmp/gedit.gsettings
sudoFunc () {
# Must be running as sudo
if [[ $(id -u) != 0 ]]; then
zenity --error --text "Sudo password authentication failed. Aborting."
exit 99
fi
# Get sudo's gedit preferences
gsettings list-recursively | grep -i gedit | grep -v history | \
grep -v docinfo | \
grep -v virtual-root | grep -v state.window > /tmp/gedit.gsettings.root
diff /tmp/gedit.gsettings.root /tmp/gedit.gsettings | grep '>' > /tmp/gedit.gsettings.diff
sed -i 's/>/gsettings set/g; s/uint32 //g' /tmp/gedit.gsettings.diff
chmod +x /tmp/gedit.gsettings.diff
bash -x /tmp/gedit.gsettings.diff # Display override setting to terminal
# nohup gedit $@ &>/dev/null &
nohup gedit -g 1300x840+1+1220 $@ &>/dev/null &
# Set the X geometry window size (WIDTHxHEIGHT+X+Y).
}
FUNC=$(declare -f sudoFunc)
sudo -H bash -c "$FUNC; sudoFunc $*;"
exit 0
The script must be called in regular user mode. It copies gsettings
for gedit
from user profile to /tmp
. Important settings like font size, line wrap, tab settings, convert tabs to spaces and plug-ins are copied.
Then sudo
password is requested and status is elevated to root.
sudo -H
is used for equivalent to gksu
protection to prevent root
powers from hammering user configuration files.
Next root configuration settings for gedit
are inherited from calling user's profile that was copied into /tmp
.
gedit
is loaded as a background task and user is presented with sudo
version of file opened. For example /etc/default/grub
.
The sudo
powers are immediately dropped and the command line prompt returns. However gedit
is still running in a separate window with file opened for editing.