是否可以获取有关运行 bash 脚本的终端窗口/选项卡的状态信息?
如果已经有人问过这个问题,请提前道歉 - 搜索这个问题真的很难,因为 bash 和终端同时出现在很多地方。
基本上我想要做的是让 bash 脚本知道它运行的终端当前是否处于活动状态(选定),以便我可以使用通知发送来通知用户只有在以下情况下某些操作已完成它当前不是活动窗口。我猜想可以做一些事情,至少可以通过以某种方式访问 gnome 来检测活动窗口是否是终端窗口,但我不知道如何做,我想这可能是一个黑客,但任何帮助都是赞赏。
Apologies in advance if this has already been asked - it's really hard to search for this as bash and terminal show up together in so many places.
Basically what I'd like to be able to do is have a bash script be aware of whether or not the terminal it's running in is currently active (selected) so that I could use notify-send to inform the user something has completed only if it's not currently the active window. I'm guessing it would be possible to do something that can at least detect whether the active window is a terminal window by accessing gnome somehow, but I have no idea how and I imagine it would likely be a hack, but any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题主要是关于 xserver 和窗口管理,而不是 bash 本身。为此,您需要两件事:
a) 找到您的窗口:
它给出如下输出:
这是窗口堆栈,因此最后一个窗口位于顶部。您需要提取窗口 ID 列表 (0xe0001e 0x3a0006d 0x1a00004)。
接下来,您必须迭代该 id 列表。您可以通过检查此属性来找到您的窗户:
b) 如何升起窗户的示例如下:
X11:通过命令行提升现有窗口?
Well this question is rather about xserver and window management then bash itself. You need 2 things for this:
a) to find your window:
It gives an output like:
This is window stack so the last window is on top. You need to extract list of window ids (0xe0001e 0x3a0006d 0x1a00004).
Next you have to iterate over that list of ids. You will find your window by examining this property:
b) Example how to raise your window is here:
X11: raise an existing window via command line?
经过更多挖掘后,我找到了一个可行的解决方案,因此我将其作为答案发布,但它不太便携,因此我仍然希望有任何更好的解决方案。
我发现,当使用 Gnome 时,您可以从 gnome 终端访问 $WINDOWID 变量(不幸的是,如果您已经 ssh 到另一个盒子,那么这将不起作用,因此并不理想)。这可以与 xprop 结合起来,如下所示:
然后可以比较其是否相等。然而,获取当前窗口 id 的更合适的方法会更好。
编辑:看到 Zaytzev 的答案后,我想这可以合并,这样我就可以设置终端标题并检查 xprop -id $activeWindow WM_NAME 是否等于我的窗口标题,而不是使用 $WINDOWID 。
After some more digging I've found a workable solution so I'm going to post it as an answer, but it's not very portable so I'd still appreciate any better solutions.
What I've found is that when using Gnome you have access to a $WINDOWID variable from the gnome-terminal (this doesn't work if you've ssh'ed to another box unfortunately and so isn't ideal). This can be combined with xprop like this:
Which can then be compared for equality. A more suitable way of getting the current window id would be good however.
Edit: Having seen Zaytzev's answer I guess this could be combined so instead of using $WINDOWID I could set the terminal title and check whether
xprop -id $activeWindow WM_NAME
is equal to my window title.