对于您的问题标题中的一般情况,这可以通过至少两种方式单独在 bash 中完成。
一个使用bash 字符串操作:
# ${VARIABLE##pattern} trims the longest match from the start of the variable.
# This assumes that "window:nnnnnn" is the last property returned.
DOTOOL_OUTPUT=$(xdotool getmouselocation)
WINDOW_HANDLE=${DOTOOL_OUTPUT##*window:}
作为助记符,#
位于键盘左侧$
并修剪字符串的开头;%
位于字符串的右侧$
并修剪字符串的末尾。#
并%
修剪最短的模式匹配;##
并%%
修剪最长。
另一种方式使用bash 正则表达式匹配:
# Within bash's [[ ]] construct, which is a built-in replacement for
# test and [ ], you can use =~ to match regular expressions. Their
# matching groups will be listed in the BASH_REMATCH array.
# Accessing arrays in bash requires braces (i.e. ${ } syntax).
DOTOOL_OUTPUT=$(xdotool getmouselocation)
if [[ $XDOTOOL_OUTPUT =~ window:([0-9]+) ]]; then
WINDOW_HANDLE=${BASH_REMATCH[1]}
fi