如何阻止 gedit(和其他程序)在我的终端中输出 GTK 警告等?

FUZ*_*xxl 36 gedit gtk

从 raring 升级后,我在 trusty 上运行了很棒的窗口管理器。我的桌面环境故意没有运行所有的 Gnome / Freedesktop 守护进程——我不想要它们。

当我gedit从这样的终端执行时:

gedit file
Run Code Online (Sandbox Code Playgroud)

每当我按回车键或保存或在其他各种情况下,它都会在我的终端上输出这样的消息:

(gedit:5700): Gtk-WARNING **: Calling Inhibit failed: GDBus.Error:org.freedesktop.DBus.Error.ServiceUnknown: The name org.gnome.SessionManager was not provided by any .service files
Run Code Online (Sandbox Code Playgroud)

我明白这个警告的意思,我决定这对我来说无关紧要。

我怎样才能关闭这种警告?我所说的“关闭”并不是指任何这些或类似的解决方法:

  • 将 gedit 的输出通过管道传输到 /dev/null
  • 编写一个包装脚本,将 gedit 的输出通过管道传输到 /dev/null
  • 创建一个别名,将 gedit 的输出传送到 /dev/null

这些变通方法是不可接受的,因为它们必须单独应用于每个 Gnome 应用程序 — gedit 并不是唯一喜欢弄乱终端的应用程序。

avi*_*vih 18

首先,我还发现这些警告显示在开箱即用的 Ubuntu 上很烦人,没有“适当”的方法来禁用它们,我可以找到(似乎最常见的“解决方案”是安装gir1.2-gtksource-3.0这似乎不起作用,因为它已经安装,或者忽略它们 - 但我想完全抑制它们,因为它们只会让我的终端嘈杂)。

我想出了以下代码,到目前为止,它的行为似乎完全符合我的预期,并且基于 TuKsn 的回答,但对其进行了一些增强:

  • 默认情况下 ( gedit ...) 无需使用 F12 或其他一些快捷方式(调用未过滤的 use /usr/bin/gedit ...)即可工作。
  • 当它作为后台任务终止时显示输入的命令名称。

仍然可以概括一下,但是现在,如果您需要对其他命令进行相同处理,请​​为gedit()需要相同过滤器的每个其他命令名称复制该功能。

# solution adapted from: http://askubuntu.com/questions/505594
# TODO: use a list of warnings instead of cramming all of them to a single grep.
# TODO: generalize gedit() to allow the same treatment for several commands
#       without duplicating the function with only a different name
# output filter. takes: name_for_history some_command [arguments]
# the first argument is required both for history, but also when invoking to bg
# such that it shows Done <name> ... instead of e.g. Done /usr/bin/gedit ...
suppress-gnome-warnings() {
    # $1 is the name which should appear on history but is otherwise unused.
    historyName=$1
    shift

    if [ -n "$*" ]; then
        # write the real command to history without the prefix
        # syntax adapted from http://stackoverflow.com/questions/4827690
        history -s "$historyName ${@:2}"

        # catch the command output
        errorMsg=$( $* 2>&1 )

        # check if the command output contains not a (one of two) GTK-Warnings
        if ! $(echo $errorMsg | grep -q 'Gtk-WARNING\|connect to accessibility bus'); then
            echo $errorMsg
        fi
    fi
}
gedit() {
  suppress-gnome-warnings $FUNCNAME $(which $FUNCNAME) $@
}
Run Code Online (Sandbox Code Playgroud)

还有一个更好的版本(更小,完全通用,自调用以来无需重写历史记录,并且更好地过滤每行而不是整个输出):

# generates a function named $1 which:
# - executes $(which $1) [with args]
# - suppresses output lines which match $2
# e.g. adding: _supress echo "hello\|world"
# will generate this function:
# echo() { $(which echo) "$@" 2>&1 | tr -d '\r' | grep -v "hello\|world"; }
# and from now on, using echo will work normally except that lines with
# hello or world will not show at the output
# to see the generated functions, replace eval with echo below
# the 'tr' filter makes sure no spurious empty lines pass from some commands
_supress() {
  eval "$1() { \$(which $1) \"\$@\" 2>&1 | tr -d '\r' | grep -v \"$2\"; }"
}

_supress gedit          "Gtk-WARNING\|connect to accessibility bus"
_supress gnome-terminal "accessibility bus\|stop working with a future version"
_supress firefox        "g_slice_set_config"
Run Code Online (Sandbox Code Playgroud)