AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题 / 780787
Accepted
Luis A. Florit
Luis A. Florit
Asked: 2024-07-25 22:54:22 +0800 CST2024-07-25 22:54:22 +0800 CST 2024-07-25 22:54:22 +0800 CST

根控制台出现奇怪的 bash 错误

  • 772

经常,当我使用 切换 root 用户时,控制台中甚至 TTY 中su -都会出现奇怪的bash错误消息。即使在从头开始进行完整的系统升级后,这种情况仍然会发生,因此这可能是HOME 中的问题。rxvtroot

例如,这些:

sh: error importing function definition for `_file'
sh: _file: line 3: syntax error near unexpected token `('
sh: _file: line 3: ` --help | --version | --separator | -!(-*)[vF])'

当我注销时,出现几个错误,例如:

/bin/bash: _sudo: line 19: syntax error near unexpected token `('
/bin/bash: _sudo: line 19: ` --user | --other-user | -!(-*)[uU])'
/bin/bash: error importing function definition for `_sudo'

即使我sh在控制台中这样做,我也会得到

/bin/bash: _file: line 3: syntax error near unexpected token `('
/bin/bash: _file: line 3: ` --help | --version | --separator | -!(-*)[vF])'
/bin/bash: error importing function definition for `_file'

然后我注销并再次登录,恢复正常行为。直到它开始表现得很奇怪……

我以为这是“~/.bashrc”或类似文件的问题,但这些文件与我的普通用户共享,而普通用户从来不会在控制台中显示错误。

显然有一个bash脚本有错误,但我甚至不知道从哪里开始查找。这是什么_sudo?命令什么grep _sudo $(find . -type f)也find . -name _sudo没显示...

编辑

这是我的文件的内容/usr/share/bash-completion/completions/sudo:

# bash completion for sudo(8)                              -*- shell-script -*-

_sudo()
{
    local cur prev words cword split
    _init_completion -s || return

    local i mode=normal
    [[ $1 == *sudoedit ]] && mode=edit

    [[ $mode == normal ]] &&
        for ((i = 1; i <= cword; i++)); do
            if [[ ${words[i]} != -* ]]; then
                local PATH=$PATH:/sbin:/usr/sbin:/usr/local/sbin
                local root_command=${words[i]}
                _command_offset $i
                return
            fi
            if [[ ${words[i]} == -@(!(-*)e*|-edit) ]]; then
                mode=edit
                break
            fi
            [[ ${words[i]} == \
            -@(user|other-user|group|close-from|prompt|!(-*)[uUgCp]) ]] &&
                ((i++))
        done

    case "$prev" in
        --user | --other-user | -!(-*)[uU])
            COMPREPLY=($(compgen -u -- "$cur"))
            return
            ;;
        --group | -!(-*)g)
            COMPREPLY=($(compgen -g -- "$cur"))
            return
            ;;
        --close-from | --prompt | -!(-*)[Cp])
            return
            ;;
    esac

    $split && return

    if [[ $cur == -* ]]; then
        local opts=$(_parse_help "$1")
        COMPREPLY=($(compgen -W '${opts:-$(_parse_usage "$1")}' -- "$cur"))
        [[ ${COMPREPLY-} == *= ]] && compopt -o nospace
        return
    fi
    if [[ $mode == edit ]]; then
        _filedir
    fi
} &&
    complete -F _sudo sudo sudoedit

# ex: filetype=sh
linux
  • 1 1 个回答
  • 109 Views

1 个回答

  • Voted
  1. Best Answer
    Vilinkameni
    2024-08-26T19:01:48+08:002024-08-26T19:01:48+08:00

    为了正式回答这个问题,正如@steeldriver 在评论中提到的那样,这是来自项目的completions/sudo_sudo文件函数的名称。bash-completion

    source当尝试使用该文件作为参数在 Bash 中使用点或实用程序时,会出现给定的错误消息,并且extglob未设置选项(这是默认设置¹)。发生这种情况的原因是第 19 行包含扩展模式匹配运算符@()和!():

    if [[ ${words[i]} == -@(!(-*)e*|-edit) ]]; then
    

    但是,正如@Stéphane Chazelas 指出的那样(见脚注),自 Bash 4.1 以来,扩展模式匹配运算符应该在[[ ... ]]右侧工作==,因此这可能不是问题的真正根源。根据给出的错误消息,有问题的行似乎是第 29 行:

    --user | --other-user | -!(-*)[uU])
    

    类似地,在文件completions/file!()中使用:

    --help | --version | --separator | -!(-*)[vF])
    

    以下是使用来自 OpenBSD 的文件路径的点实用程序的示例用法(其他系统上的具体位置可能有所不同):

    . /usr/local/share/bash-completion/completions/sudo
    

    类似上述的命令可能包含在其中一个初始化文件中,或者从中调用的脚本中。

    该 (已弃用)选项-指示 su(1) 将相关用户的 shell(根据注释,Bash)(不带参数,root)作为(交互式)登录 shell 执行。

    当作为交互式登录 shell 执行时,Bash 会获取/etc/profile,然后按顺序获取以下列表中的第一个现有文件:

    1. 〜/ .bash_profile,
    2. ~/.bash_login,或
    3. 配置文件。

    对于 root 用户,替换~为/root:

    1. /root/.bash_profile,
    2. /root/.bash_login,或
    3. 您可以使用 /root/.profile来配置配置文件。

    连同/etc/profile一起,这些是用于检查文件Completions/sudo来源的文件。一行

    shopt -s extglob
    

    应添加到该文件中,在完成/sudo的来源之前。

    注意:这可能会影响安全性,因为它允许 root 的 shell 使用扩展的通配符,而扩展的通配符原本是关闭的。


    ¹@Stéphane Chazelas 的注释:自 bash 4.1extglob运算符(ksh 扩展的 glob 运算符的子集)起[[...]],无论该选项是否全局启用,都可以在模式匹配运算符中使用。

    • 2

相关问题

  • 有没有办法让 ls 只显示某些目录的隐藏文件?

  • 使用键盘快捷键启动/停止 systemd 服务 [关闭]

  • 需要一些系统调用

  • astyle 不会更改源文件格式

  • 通过标签将根文件系统传递给linux内核

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve