0

I am trying to write a script that moves the active window to the next higher workspace, which I will then assign to a key. I am using xdotool to find the active window, then wmctrl to list the active windows, and want to use grep to find the active window in the list of windows to find out which workspace it's on, so that I can then increment that by 1 and move the window to that workspace if it exists.

As you can see, searching for that value as a variable ACTWIND or as a value 0x5a00004 (and variations in single or double quotes, with and without beginning of line) results in no lines found, but when I search for LXTerminal which is on the same line, it is found. In my script I will need it to either work using a variable or by substituting in the xdotool call where the variable would be.

ACTWIND=$( printf 0x%x $(xdotool getactivewindow) )

$ echo “123 $ACTWIND 456”
123 0x5a00004 456

$ wmctrl -l
0x03600001 0 hpdv9917d Conky (hpdv9917d)
0x02800010 0 hpdv9917d YiPs Wiki (i powered) – Search – Mozilla Firefox
0x02800027 0 hpdv9917d Print all variables in a class? – Python – Stack Overflow – Mozilla Firefox
0x02800038 0 hpdv9917d (5) antiX (and MX) frugal installs (with grub entry) – YouTube – Mozilla Firefox
0x05a00004 0 hpdv9917d LXTerminal
0x02000002 0 hpdv9917d mrxvt-mini
0x02800043 0 hpdv9917d how to shift applications from workspace 1 to 2 using command – Ask Ubuntu – Mozilla Firefox
0x02600003 0 hpdv9917d *untitled – Geany
0x03200003 0 hpdv9917d antiX Control Center
0x03e00002 0 hpdv9917d alsamixer
0x01000008 0 hpdv9917d /home/bobc/Downloads/work

$ wmctrl -d
0 * DG: 1440×900 VP: 0,0 WA: 0,0 1440×875 N/A
1 – DG: 1440×900 VP: 0,0 WA: 0,0 1440×875 N/A

$ wmctrl -l | grep $ACTWIND

$ wmctrl -l | grep 0x5a00004

$ wmctrl -l | grep "0x5a00004"

$ wmctrl -l | grep '0x5a00004'

$ wmctrl -l | grep '^0x5a00004'

$ wmctrl -l | grep "^0x5a00004"

$ wmctrl -l | grep LXTerminal
0x05a00004  0 hpdv9917d LXTerminal
  • 5
    Looks like it's 0x05a00004, not 0x5a. – kevlinux Jan 26 at 19:08
4

The string you're grepping with should be 0x05a..., not 0x5a....

You may want to change how you assign to your variable, maybe to something like

active_window=$( printf '0x%08x' "$( xdotool getactivewindow )" )

When you want to do an exact string match with grep (and not a regular expression match), use -F:

grep -F '0x05a00004'

In this case though, I would probably want to anchor the expression at the start of the line:

grep '^0x05a00004'

or

grep "^$active_window"

or possibly even

awk -v str="$active_window" '$1 == str'

for an exact string match against the first whitespace-delimited field.

  • Whether awk -v str="$active_window" '$1 == str' does a string or number comparison will depend on the awk implementation (and in the case of gawk whether $POSIXLY_CORRECT is in the environment as some versions of the POSIX specification inadvertently required strings like 0x05a00004 to be recognised as numbers). – Stéphane Chazelas Jan 26 at 22:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.