SYSEdit - System File Editor - Version 1.00

SYSEdit is a bash script written to be launched from the KDE or GNOME desktop to allow the direct edit of any text file. If the file belongs to root, you will be asked for a password, if it belongs to you, you will not be asked for a password. SYSEdit will launch kdialog if you use KDE and zenity if you use GNOME. You can edit any system or personnel file without changing the owner. With KDE, KWRITE is used as your text editor while in GNOME, GEDIT will be used. SYSEdit is written to determine your dekstop between KDE & GNOME and use the correct commands. As always, you can edit SYSEdit and modify its defaults. SYSEdit will create a desktop icon for you, usable in KDE and GNOME 2, but not in GNOME 3 as I understand it. If you do not want an icon, change sysiconcheck=true to sysiconcheck=false, before you run the script. SYSEdit by default will create a backup text file before you edit the original. If you do not want a backup then change the setting backup=true to backup=false.

To create SYSEdit, copy and past the following text into your favorite text editor as the file sysedit in the ~/bin folder (/hime/username/bin/sysedit):

#!/bin/bash

#: Title       : sysedit
#: Date Created: Thu Oct 6 19:29:53 CDT 2011
#: Last Edit   : Fri Oct 7 18:25:00 CDT 2011
#: Author      : James D. McDaniel
#: Version     : 1.00
#: Description : Select Text File To Edit from Desktop
#: Options     : None (Do not enter any options)

#
# This bash script is written to be run from the kde
# or gnome desktop and not from a terminal session.
#

#
# Created for openSUSE forums on Friday October 7, 2011
#

TITLE="SYSEdit - System File Editor - Version 1.00"

#
# Copy and Paste the text of this script into a text editor and  
# save it as the text file sysedit in the /home area bin folder 
# example is: /home/username/bin, also known as ~/bin
# This script must be marked executable to be used.  Please  
# run the following Terminal command: chmod +x ~/bin/sysedit
# sysedit was designed to be run from inside your desktop.
#

#
# Do you want a back up text file made before you edit the file?
#
backup=true

#
# sysedit can create a kde desktop icon if you want ***************************
#
sysiconcheck=true
sysediticon="$HOME/Desktop/SYSEdit.desktop"

#
# Check/Create Sysedit Icon on the desktop
#
function deskicon {
if  ! -e $sysediticon ] ; then
  cat >> "$sysediticon" << "EOFTEXT"
[Desktop Entry]
Comment[en_US]=
Comment=
Exec=sysedit
GenericName[en_US]=
GenericName=
Icon=accessories-text-editor
MimeType=
Name[en_US]=SYSEdit
Name=SYSEdit
Path=/
StartupNotify=true
Terminal=false
TerminalOptions=
Type=Application
X-DBUS-ServiceName=
X-DBUS-StartupType=
X-KDE-SubstituteUID=false
X-KDE-Username=
X-SuSE-translate=true
EOFTEXT
  chmod +x $sysediticon
fi
}

#
# Check to see if desktop icon presence should be checked *********************
#
if $sysiconcheck ; then
  deskicon
fi

#
# This is the standard GPL Statement, leave at the top of the script.
# Just use the command show_gpl after this function for it to be shown.
#
function show_gpl {
echo $TITLE
echo ""
echo "sysedit is a bash script file written to be used with openSUSE."
echo "Copyright (C) 2011 by James D. McDaniel, jmcdaniel3@austin.rr.com"
echo ""
echo "This program is free software; you can redistribute it and/or modify"
echo "it under the terms of the GNU General Public License as published by"
echo "the Free Software Foundation; either version 2 of the License, or"
echo "(at your option) any later version."
echo ""
echo "This program is distributed in the hope that it will be useful,"
echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the"
echo "GNU General Public License for more details."
echo ""
echo "You should have received a copy of the GNU General Public License"
echo "along with this program; if not, write to the Free Software"
echo "Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA"
echo ""
}

#
# Exit sysedit due to a detected error ****************************************
#
function Sudden_Exit {
  clear
  show_gpl
  echo "Desktop: $Desktop & Exit Code: "$Exit_Code
  echo
  exit 1
}

#
# Determine which desktop that we are using ***********************************
#
de=0
ps nc -C plasma-desktop >/dev/null && de=1     # For kde
ps nc -C gnome-panel >/dev/null && de=2     # For gnome2
ps nc -C gnome-shell >/dev/null && de=3     # For gnome3

#
# Insert the Name Of your Favorite KDE or GNOME File Editor
# If a KDE or GNOME desktop is not found, then we just do an exit 1!
#
case "$de" in

  "1") Desktop="KDE"     ; EDITOR="kwrite"     ;  SU_CMD="kdesu"     ;; # For KDE
  "2") Desktop="GNOME"    ; EDITOR="gedit"     ;  SU_CMD="gnomesu"     ;; # For gnome2
  "3") Desktop="GNOME"    ; EDITOR="gedit"     ;  SU_CMD="gnomesu"     ;; # For gnome3
  *) Sudden_Exit ;;

esac

#
# You may force the use of kdialog or zenity if you wish **********************
#
# Desktop="KDE"    # to use kdialog
# Desktop="GNOME" # to use zenity

#
# Check for the presence of kdialog or zenity, based on your Desktop
# Exit nsf if the selected window dialog program was not found installed
#
if  "$Desktop" == "KDE" ] ; then
  which kdialog > /dev/null
  Exit_Code=$?
fi
if  "$Desktop" == "GNOME" ] ; then
  which zenity > /dev/null
  Exit_Code=$?
fi
if  ! $Exit_Code -eq 0 ] ; then
  Sudden_Exit
fi

#
# Request File name to be edited **********************************************
#
if  "$1" == "" ] ; then
  if  "$Desktop" == "GNOME" ] ; then
    FILE=$(zenity --file-selection --title "Select a Text File to Edit")
    Exit_Code=$?
  else
    FILE=$(kdialog --getopenfilename ~/ --title "Select a Text File to Edit")
    Exit_Code=$?
  fi
else
  FILE=$1
fi

#
# Check Dialog Exit Code ******************************************************
#
if  ! $Exit_Code -eq 0 ] ; then
  Sudden_Exit
fi

#
# make sure file exists *******************************************************
#
if  ! -e $FILE ] ; then
  if  "$Desktop" == "GNOME" ] ; then
    zenity --error --text="The File $FILE Does Not Exist!
 Press OK to Exit"
  else
    kdialog --sorry "The File $FILE Does Not Exist!
 Press OK to Exit"
  fi
  Sudden_Exit
fi

#
# Determine File Owner & Edit as Root Only if the File Belongs to Root ********
#
OWNER=$(/bin/stat -c %U "$FILE")

if  "$OWNER" == "root" ]  ; then
  if  $UID != 0 ] ; then
    $SU_CMD $0 "$FILE"
    exit 0
  fi
fi

if $BACKUP ; then

#
# Create File Extension for Backup File ***************************************
#
  ext=$(date +%y%m%d.%H%M%S)
  BACK=$FILE.$ext

#
# Make a Backup Copy of the File Before the File is edited ********************
#
  cp "$FILE" "$BACK"

fi

#
# Edit the file command is here ***********************************************
#
$EDITOR "$FILE"

clear
show_gpl    # Only shows up if ran from the terminal prompt

exit 0

# End Of Script

SYSEdit must be marked executable in order to be useable. Open up a terminal session after you have saved SYSEdit and run the following command:

chmod +x ~/bin/sysedit

To use SYSEdit, you can run the terminal command:

sysedit

Or, you can do an Alt-F2 in KDE and enter sysedit. Once the icon is on your desktop, just click it with your mouse to start SYSEdit. As always, I want to hear any questions or comments about using SYSEdit.

Thank You,