首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用GUI和CLI文本编辑器#2在Linux (作为root)中编辑系统文件

使用GUI和CLI文本编辑器#2在Linux (作为root)中编辑系统文件
EN

Code Review用户
提问于 2020-02-02 11:21:58
回答 2查看 135关注 0票数 1

,这是这个问题的第二次迭代,

首先要注意:这个问题的第一次迭代可以在这里找到:用GUI和CLI文本编辑器在Linux中编辑系统文件(作为root)

如该文件所述:

我的目的是编写一个通用函数,用于通过POSIX-ly运行用于不同目的的各种文本编辑器,即将文件作为根安全地编辑。例如,如果在文件编辑过程中出现了电源丢失,则另一个示例可能会丢失SSH连接,等等。

我现在向你们提出一个可能的最终解决办法:

sudoedit增强

代码语言:javascript
复制
# 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页面上的示例图像。

EN

回答 2

Code Review用户

回答已采纳

发布于 2020-02-06 11:53:32

自评

注释

注释对于未来的读者来说是非常重要的,它们加快了对整个代码的理解。我想我至少搞错了一条评论(其他待审):

  • 让我们仔细看一下第一个论点,编辑
  • 建议:#存储第一个参数,编辑器名

合并可以组合的

通过将简单的代码组合在一起,我们可以更容易地阅读。

  • #存储编辑器别名,如果有任何editor_alias=$(别名"$editor_name“2> /dev/null )#删除该别名(如果;则取消别名"$editor_name”fi )
  • 建议:#存储编辑器别名;如果有编辑器别名,则暂时删除它,如果editor_alias=$(别名"$editor_name“2> /dev/null );然后删除"$editor_name”fi

在函数

中实现代码解决方案

我以前的解决方案没有对code进行任何检查,而且,通过这样做,我们可以去掉那个外星别名。

  • # VS代码特定的解决方案,在根别名sucode=下工作“sudo -p / root /..vscode && sudo Code -w --user dir=/root/..vscode”
  • 建议:#使用一次性SUDO_EDITOR设置运行编辑器if;然后#代码特定于sudoedit -p /root/..vscode && sudo代码-w --user dir=/root/..vscode#$@# main命令泛型SUDO_EDITOR="$editor_path $wait_option“

避免生成未安装

编辑器的编辑器别名

我以前的解决方案是生成所有的编辑器别名,不管这类程序是否安装在系统上,这可能会让用户感到不快。

  • cli_editor in $sudoedit__cli_editor_list;做别名su$cli_editor="sudoedit_enhanced_run $cli_editor‘“gui_editor in $sudoedit__gui_editor_list;做别名su$gui_editor="sudoedit_enhanced_run $gui_editor -w”
  • 建议:对于cli_editor in $sudoedit__cli_editor_list;do if命令" $cli_editor“> /dev/null 2>&1;然后别名su$cli_editor="sudoedit_enhanced_run $cli_editor ''”fi for gui_editor in $sudoedit__gui_editor_list;do if命令-v " $gui_editor“> /dev/null 2>&1;然后别名su$gui_editor="sudoedit_enhanced_run $gui_editor -w”
票数 3
EN

Code Review用户

发布于 2020-02-06 17:31:58

这条长队:

代码语言:javascript
复制
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 )和在不需要展开的地方使用单引号(避免编写\$),可以很容易地使其更易于处理:

代码语言:javascript
复制
# 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 $#: $*"
票数 2
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/236530

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档