如何使用 dbus-monitor 和 xargs 解决“不匹配的双引号”错误?

Jac*_*ijm 8 command-line scripts dbus xargs

为了notify-osd在 Linux (Ubuntu) 上拦截 ( ) 通知,我使用了dbus-monitor下面的脚本。随后,脚本运行另一个脚本 ( /opt/nonotifs/nonotifs/silent),并将截获的通知作为参数,以进行进一步处理:

#!/bin/bash

dbus-monitor "interface='org.freedesktop.Notifications'" | \
grep --line-buffered "string" | \
grep --line-buffered -e method -e ":" -e '""' -e urgency -e notify -v | \
grep --line-buffered '.*(?=string)|(?<=string).*' -oPi | \
grep --line-buffered -v '^\s*$' | \
xargs -I '{}' /opt/nonotifs/nonotifs/silent {}
Run Code Online (Sandbox Code Playgroud)

这完美无缺,除了通知hplip

在此处输入图片说明

从终端运行时,上面的脚本显示:

xargs: unmatched double quote; by default quotes are special to xargs unless you use the -0 option
Run Code Online (Sandbox Code Playgroud)

-0但是,在使用该选项时,脚本根本不提供任何参数。

我试过的

在某些情况下,脚本随后会中断。如果情况总是如此,可以通过在“保持活动状态”包装器中运行它来解决它,我试过。然而,通常情况下,脚本不会终止,但它仍然停止返回截获的通知。

我该如何解决这个问题?

编辑

正如@Serg 所建议的,我将该xargs...部分替换为cat -A, 以查看传递给xargs. 这表明(第三行)的通知中确实存在不匹配的双引号hplip,这似乎是通知中的错误。

运行时的输出cat -A,调用通知:

"hplip"$ 
"HPLIP Device Status"$ 
"Officejet_Pro_8600$ 
"transient"$
Run Code Online (Sandbox Code Playgroud)

mur*_*uru 7

man xargs

--分隔符=delim
-d 德利姆
      输入项以指定字符结束。  引号
      和反斜杠并不特殊;输入中的每个字符都是
      从字面上看。   禁用文件结束字符串,即
      像任何其他论点一样对待。当
      输入仅由换行符分隔的项目组成,尽管它是
      几乎总是更好地设计您的程序以在其中使用 --null
      这个有可能。指定的分隔符可以是单个
      字符,C 风格的字符转义符,例如 \n,或者八进制或
      十六进制转义码。八进制和十六进制转义码是
      理解为 printf 命令。多字节字符是
      不支持。

举个例子:

$ echo '"""' | xargs
\xargs: unmatched double quote; by default quotes are special to xargs unless you use the -0 option
$ echo '"""' | xargs -d '\n'
"""

$ echo '"""' | xargs -d ' ' 
"""
Run Code Online (Sandbox Code Playgroud)

当然,使用任何一个都可能会破坏一些东西,但可能不会像-0.