如何通过bash在LinuxMint中查询bash脚本为运行的虚拟桌面数量?

How can I query the number of the virtual desktop on which the bash script is running in Linux Mint via 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.

根据 KamilCuk 的回答,可以按照以下方式输出包含活动桌面数量的行:

nr_of_active_desktop=activedesktop=$(wmctrl -d | grep "*" | rev | cut -d ' ' -f1)
echo $nr_of_active_desktop

遵循需要awk的解决方案:

nr_of_active_workspace=$(wmctrl -d | grep "*" | awk '{print }')

echo $nr_of_active_workspace

它可以是一个不需要 awk 的解决方案,也可以通过其他方式实现。

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