首先要注意:这个问题的第一次迭代可以在这里找到:用GUI和CLI文本编辑器在Linux中编辑系统文件(作为root)
如该文件所述:
我的目的是编写一个通用函数,用于通过POSIX-ly运行用于不同目的的各种文本编辑器,即将文件作为根安全地编辑。例如,如果在文件编辑过程中出现了电源丢失,则另一个示例可能会丢失SSH连接,等等。
我现在向你们提出一个可能的最终解决办法:
sudoedit增强# 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; #qcStackCode#2 = wait option; 3, (#qcStackCode#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"我已经在GitHub上发布了,而我也尽力描述了它的用途和(尽管这是显而易见的)用法,包括一些在项目GitHub页面上的示例图像。
发布于 2020-02-06 11:53:32
注释对于未来的读者来说是非常重要的,它们加快了对整个代码的理解。我想我至少搞错了一条评论(其他待审):
通过将简单的代码组合在一起,我们可以更容易地阅读。
中实现代码解决方案
我以前的解决方案没有对code进行任何检查,而且,通过这样做,我们可以去掉那个外星别名。
编辑器的编辑器别名
我以前的解决方案是生成所有的编辑器别名,不管这类程序是否安装在系统上,这可能会让用户感到不快。
发布于 2020-02-06 17:31:58
这条长队:
printf '%b\n' "sudoedit_enhanced_run(): Low number of arguments.\\nExpected: 1 = editor name; #qcStackCode#2 = wait option; 3, (#qcStackCode#4), ... = file(s).\\nPassed $#: $*" >&2通过分隔行(因为我们在格式字符串中有\n )和在不需要展开的地方使用单引号(避免编写\$),可以很容易地使其更易于处理:
# shellcheck disable=SC2016
printf '%s\n' >&2 \
'sudoedit_enhanced_run(): Low number of arguments.' \
'Expected: $1 = editor name; $2 = wait option; $3, ($4), ... = file(s).' \
"Passed $#: $*"https://codereview.stackexchange.com/questions/236530
复制相似问题