如何通过管道将 stderr 发送到通知发送?

How to pipe stderr to notify-send?

我 运行 使用键绑定的程序,它们本身可以调用 dmenu 并在后台执行操作。我很乐意将 stderr 传送到 notify-send 并在出现故障时收到通知。

这些程序不是从终端仿真器调用的,主要是 sxhkd coupled with an xdg-open alternative jaro

就拿这个例子来说吧:

$ ls /root
"/root": Permission denied (os error 13)
$ ls /root 2> /dev/null
$

如果我有 sxhkd 条目

super + Return
  ls /root

如何让它输出 stderr 到通知?

通知发送不适用于 piping

不过,您可以尝试: sudo pip install notify-pipe

接受piping

看这里:https://github.com/ron7/notify-pipe

并且您可以通过管道将 stderr 传输到 stdout 并将其发送到通知管道

command 2>&1|notify-pipe

创建脚本notify-pipe:

#!/usr/bin/env sh

read notification
notify-send "Command Failed" "$notification" "$@"

然后通过管道 stderrstdout:

$ ls /root  2>&1| notify-pipe

可能的改进:bash get command that was used before pipe symbol在通知摘要中获取命令。

感谢罗恩的帮助。