2

I try to check whether there is a window with a certain title ("Enter password") in the list of windows.

To understand whether my script works, I'd like to print the result of the comparison.

Here is my code:

windows=$(wmctrl -l)
echo $(( "$windows" =~ "Enter password" ))

Here is the error, I get:

bash: 0x00e00006 -1 Laptopname Enter password for =~ password: Syntaxfehler im Ausdruck. (fehlerverursachendes Zeichen ist "password for =~ Enter password")

Translation: Syntax error in the expression. (Character causing the error is "password for =~ Enter password")

How to do it properly?

2 Answers 2

4

Since you're listing window titles using wmctrl -l, it'd make more sense to immediately check if a window title in the output starts with Enter password instead of storing the whole output in order to process it further.

According to man wmctrl, when passing -l without any other option, the fourth space-separated field will always contain the title (although the first field seems to always contain a trailing space).

Using cut + grep:

wmctrl -l | cut -d' ' --complement -f1-4 | grep -q '^Enter password' && echo yes || echo no

Will echo yes if a window title starts with Enter password or no otherwise.

4

You need to use [[]] for the =~ operator. However, it cannot be used as a parameter to echo, but in an if statement like this:

windows=$(wmctrl -l)
if [[ "$windows" =~ "Enter password" ]] ; then
  echo 'Yes, a window is present!'
else
  echo 'No such window was found!'
fi

If you really want to get a 0 or a 1 depending on the result of [[]] operator, you can use the following shorthand:

[[ "$windows" =~ "Enter password" ]] ; echo $?

Here, 0 would mean a match (=success), 1 no match (=failure).


For more details about the =~ operator see this.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .