首页 > 解决方案 > 如何通过 bash 在 Linux Mint 中查询运行 bash 脚本的虚拟桌面的编号?

问题描述

环境:

Linux Mint, Cinnamon desktop manager, with multiple workspaces=virtual desktops, e.g. 4.
Bash script

已知情况:

How to determine the number of workspaces:

wmctrl -d | wc -l

我需要的:

Get the number of virtual desktops the bash script is running on with a pure bash as var (like with grep, not awk or similar) and echo the var.

标签: linuxbashlinux-mint

解决方案


使用awk(恕我直言仍然是手头任务最合适的选择):

nr_of_active_workspace=$(wmctrl -d | awk '/\*/{print $NF}')
echo $nr_of_active_workspace

pure bash解决方案:

nr_of_active_workspace=$(wmctrl -d | while read -r line; do [[ $line =~ '*' ]] && echo ${line: -1} ; done)
echo $nr_of_active_workspace

推荐阅读