如何使用bash连续监控节奏盒的轨迹变化

如何使用bash连续监控节奏盒的轨迹变化,bash,dbus,rhythmbox,Bash,Dbus,Rhythmbox,我想做与前面描述的相同的事情,但是使用shell脚本(最好是在bash中)而不是python。使用dbus-monitor似乎可以实现这一点,但我对dbus不是很熟悉,我也不清楚如何将python问题解决方案中描述的概念应用到dbus-monitor工具中。以下是我能找到的最简单的方法: #!/bin/bash interface=org.gnome.Rhythmbox.Player member=playingUriChanged # listen for playingUriChange

我想做与前面描述的相同的事情,但是使用shell脚本(最好是在bash中)而不是python。使用
dbus-monitor
似乎可以实现这一点,但我对dbus不是很熟悉,我也不清楚如何将python问题解决方案中描述的概念应用到dbus-monitor工具中。

以下是我能找到的最简单的方法:

#!/bin/bash

interface=org.gnome.Rhythmbox.Player
member=playingUriChanged

# listen for playingUriChanged DBus events,
# each time we enter the loop, we just got an event
# so handle the event, e.g. by printing the artist and title
# see rhythmbox-client --print-playing-format for more output options

dbus-monitor --profile "interface='$interface',member='$member'" |
while read -r line; do
    printf "Now playing: "
    rhythmbox-client --print-playing
done
它产生如下输出:

Now playing: Daft Punk - Overture
Now playing: Daft Punk - The Grid
它还可以在启动时打印当前播放的歌曲。如果这不是您想要的,请查看
$line
的内容,看看它是否包含
nameaquired
playingUriChanged
。如果它包含
名称
,请跳过它


Python版本和这个bash版本之间的主要区别在于Python版本使用DBus获取播放歌曲的信息。我找不到一个使用bash的好方法,但是
rhythmbox客户端--打印播放
似乎工作得很好;如果我想在关闭Rhythmbox后退出/终止脚本,我该怎么做?在脚本中是否可能,或者我是否需要一个单独的脚本?您可以在rhythmbox命令后在循环中执行“中断”操作,以继续执行循环后的操作,Khurshid.Vyacheslav,您是否放置了
#/bin/bash
作为第一行?还要注意另一条注释:
rhythmbox客户端
在较新的系统上不存在。