This is the second iteration of this question
A note to start with: the first iteration of this question can be found here:
Editing system files in Linux (as root) with GUI and CLI text editors
As stated in there:
My intention is to POSIX-ly write one generalized function for running various text editors I use for different purposes through
sudoedit
, i.e. editing files as root safely. Safely = for instance, if a power loss occurs during the file edit; another example could be lost SSH connection, etc.
I come to you now with a possible final solution:
sudoedit
enhanced
# USAGE: Just source this file into your shell for instance if using Bash, then you can use these files: ~/.bashrc or ~/.bash_aliases
# Please, customize these lists to your preference before using this script!
sudoedit__cli_editor_list='nano vi'
sudoedit__gui_editor_list='gedit emacs xed subl' # VS Code has its own work-around
sudoedit_enhanced_run ()
# Generic function for text editing as root
# the proper, safe, way through `sudoedit`.
# Expected arguments:
# $1 = editor name; obviously mandatory
# $2 = wait option; to avoid this, pass an empty string ('')
# $3, ($4), ... = file(s); at least one file must be given
{
# check the minimum number of arguments
if [ $# -lt 3 ]; then
printf '%b\n' "sudoedit_enhanced_run(): Low number of arguments.\\nExpected: \$1 = editor name; \$2 = wait option; \$3, (\$4), ... = file(s).\\nPassed $#: $*" >&2
return 1
fi
# let's take a closer look at the first argument, the editor
editor_name=$1
# store an editor alias, if there is any
editor_alias=$( alias "$editor_name" 2> /dev/null )
# remove that alias for now
if [ -n "$editor_alias" ]; then
unalias "$editor_name"
fi
# find out if such editor exists on the system
# and store the first two arguments to variables
editor_path=$( command -v "$editor_name" 2> /dev/null )
wait_option=$2
# if that editor does not return valid path, print error and bail
if ! [ -x "$editor_path" ]; then
printf '%s\n' "sudoedit_enhanced_run(): This editor ('$editor_name') is not installed on this system." >&2
return 1
fi
# if we got here, then both of the things are ok;
# so let's move past the editor and its wait option to the actual files
shift 2
# check if all the files exist, it does not make sense to create a file this way
for file in "$@"; do
if ! [ -f "$file" ]; then
printf '%s\n' "sudoedit_enhanced_run(): This file ('$file') does not exist or it is not a regular file." >&2
return 1
fi
done
# run the editor with one-time SUDO_EDITOR set-up
SUDO_EDITOR="$editor_path $wait_option" sudoedit "$@"
# re-define the editor alias, if there was any, afterward
if [ -n "$editor_alias" ]; then
eval "$editor_alias"
fi
}
# Editor aliases generators / definitions
for cli_editor in $sudoedit__cli_editor_list; do
alias su$cli_editor="sudoedit_enhanced_run $cli_editor ''"
done
for gui_editor in $sudoedit__gui_editor_list; do
alias su$gui_editor="sudoedit_enhanced_run $gui_editor -w"
done
# VS Code specific workaround to work under root
alias sucode="sudo mkdir -p /root/.vscode && sudo code -w --user-data-dir=/root/.vscode"
Already posted on GitHub, whereas I also tried hard to describe the purpose and (in spite it shall be obvious) usage including some example images on the project GitHub page.