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 / 问题 / 677427
Accepted
user1929074
user1929074
Asked: 2021-11-14 18:08:44 +0800 CST2021-11-14 18:08:44 +0800 CST 2021-11-14 18:08:44 +0800 CST

每次匹配正则表达式时运行命令,从没有 EOF 的标准输入读取

  • 772

我正在尝试编写一个 BASH 脚本,该脚本每次在 dbus-monitor 的输出中找到某个字符串(稍后在帖子中指定的参数)时创建一个时间戳(要写入文件)。我的脚本的主要目的是在歌曲开始在 Spotify 上播放时保存时间(包括毫秒)和日期,因为它利用了通知。

string "Spotify"每当播放歌曲时,都会输出以下命令。

dbus-monitor --session interface='org.freedesktop.Notifications',member='Notify' | grep 'string "Spotify"'

我的尝试:

search='string "Spotify"'
found=$(dbus-monitor --session interface='org.freedesktop.Notifications',member='Notify' | grep 'string "Spotify"')

while [ ${search} == ${found} ]; do
    date -u +%Y%M%d-%H%M%S.%N >> timestamp.txt
done

我假设我的代码功能障碍的原因是 dbus-monitor 连续运行,因此阻止了 while 循环执行。

bash d-bus
  • 4 4 个回答
  • 737 Views

4 个回答

  • Voted
  1. cas
    2021-11-15T03:50:44+08:002021-11-15T03:50:44+08:00

    使用awk代替grep- 类似:

    dbus-monitor ... | awk '/Spotify/ {
        system("date -u +%Y%m%d-%H%M%S.%N >> timestamp.txt")
      }'
    

    (注意使用%Y%m%d代替%Y%M%D- 大写 - M 是分钟,而不是月。大写 - D 相当于%m/%d/%y)

    这将使用 awk 的system()函数在输入中看到“Spotify”时在子 shell 中运行 date 命令。或者,使用 awk 的内置日期格式和重定向:

    dbus-monitor ... | awk '/Spotify/ {
        print strftime("%Y%m%d-%H%M%S") >> "timestamp.txt"
      }'
    

    此版本不会在时间戳中打印纳秒,因为strftime()不支持%N.

    或者,使用 perl 而不是 awk。这将允许您使用 perl 的Desktop::Notify模块来获取通知或Net::DBus直接与 dbus 通信。

    • 9
  2. Stéphane Chazelas
    2021-11-15T03:54:38+08:002021-11-15T03:54:38+08:00

    由于您有 GNU 实用程序,因此您可以执行以下操作:

    dbus-monitor --session interface='org.freedesktop.Notifications',member='Notify' |
      sed -un 's/^.*string "Spotify".*$/now/p' |
      stdbuf -oL date -uf - +%Y%m%d-%H%M%S.%N >> timestamp.txt
    

    dbus-monitor已经禁用了缓冲,所以stdbuf -oL那里没有必要。

    -uGNU的选项sed会禁用输出缓冲,并使其在此处不可搜索时一次读取一个字节的输入。我们不需要后者,但我们需要前者,以便它一读取就输出一行。

    在这里,sed每次now找到包含string "Spotify".

    那now是喂给date. 使用-f -,从标准输入date读取date要打印的。对于now它读取的每一个,它以指定的格式打印当前时间。stdbuf -oL我们确保输出立即而不是分块发送到文件timestamp.txt中。

    如果您确实想运行任何任意命令,而不是仅使用 zsh/bash/ksh93 输出当前时间,您可以执行以下操作:

    while IFS= read -ru3 line || [ -n "$line" ]; do
      any arbitrary command
    done 3< <(
      dbus-monitor --session interface='org.freedesktop.Notifications',member='Notify' |
      grep --line-buffered 'string "Spotify"'
    )
    
    • 9
  3. Best Answer
    yarl
    2021-11-15T02:49:42+08:002021-11-15T02:49:42+08:00

    这应该有效:

    stdbuf -oL dbus-monitor --session interface='org.freedesktop.Notifications',member='Notify' |
    while grep -q 'string "Spotify"'; do
        date -u +%Y%M%d-%H%M%S.%N >> timestamp.txt
    done
    

    在@StéphaneChazelas 评论后编辑:

    stdbuf -oL dbus-monitor --session interface='org.freedesktop.Notifications',member='Notify' |
    grep --line-buffered 'string "Spotify"' |
    while read trash; do
        stdbuf -oL date -u +%Y%M%d-%H%M%S.%N >> timestamp.txt
    done
    

    +1 其他答案,但为了完整起见,我保留此答案

    • 3
  4. rowboat
    2021-11-15T18:52:59+08:002021-11-15T18:52:59+08:00

    在这种情况下,它看起来dbus-monitor已经提供了时间戳(以纪元时间 + 微秒为单位)。所以你可能不需要date为每场比赛执行。也许匹配表达式可以缩小范围,例如,arg0='Spotify'。

    检查输出:

    dbus-monitor "
      type='method_call',
      interface='org.freedesktop.Notifications',
      member='Notify',
      arg0='Spotify'"
    

    希望您只会看到与来自 Spotify 的通知相关的 dbus 消息(无法对此进行测试 - 这只是查看dbus 规范的猜测)。如果这可行,那么以下可能是合适的:

    dbus-monitor --profile "type='method_call',
      interface='org.freedesktop.Notifications', member='Notify', arg0='Spotify'" |
    gawk -F '\t' '
      $NF=="Notify" {
        secs = usecs = $2
        sub(/^[^.]+/,"",usecs)
        print strftime("%Y%m%d-%H%M%S",int(secs),"UTC") usecs
        fflush()
      }' > timestamp.log
    

    用于--profile输出格式,因为它似乎比默认--monitor输出更易于解析。管道到 GNUawk以提取和格式化时间戳。

    • 2

相关问题

  • 通过命令的标准输出以编程方式导出环境变量[重复]

  • 从文本文件传递变量的奇怪问题

  • 虽然行读取保持转义空间?

  • `tee` 和 `bash` 进程替换顺序

  • 运行一个非常慢的脚本直到它成功

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