I am trying to get the address of a window using wmctrl and assign it to a variable.
When I do this in a terminal (zsh)
ADDRESS=$(wmctrl -l | grep -E "Picture in picture" | awk 'NR==2 {print $1}')
echo "address is: ${ADDRESS}"
Output: address is: 0x0400001f
Which is great as that’s the expected result
However when I do the same in a script:
./pop_up.sh
#!/bin/bash
ADDRESS=$(wmctrl -l | grep -E "Picture in picture" | awk 'NR==2 {print $1}')
echo "address is: ${ADDRESS}"
Output: address is:
Nothing is printed for address even though its the same command which makes me think I am not assigning the output of the command properly or its a problem with how I am using awk (?).
What I tried:
- using backticks (“) instead of $() but still no change
- When I do ADDRESS=date, I get back the date as expected so I am not sure why assigning the output of date works but assigning the output of the command above doesn’t.
- I also tried diagnosing the issue by splitting the three commands awk, grep and wmctrl. In this case, I get the echo output of wmctrl and grep but when I reach awk it does not output the result of filtering the address from the line…
I am not too familiar with bash scripts/awk so can anyone please help me see what I am doing wrong here?
thanks
edit:
As per @EdMorton here is the edited script
#!/bin/bash
set -x;
foo=$(wmctrl -l);
echo "foo=$foo";
bar=$(wmctrl -l | grep -E "Picture in picture");
echo "bar=$bar"
ADDRESS=$(wmctrl -l | awk '/Picture in picture/ && c++{print $1; exit}')
echo "address is: ${ADDRESS}"
and the output: https://pastebin.com/wh6Wzw51
edit 2:
As @chrslg mentioned the title of the zsh terminal changes depending on what’s running in it currently therefore at the time of wmctrl, the zsh terminal has a window name of “Picture in picture” requiring two matches to get the desired result, when running from a script there are no windows with that name so it requires one match to get the result. So solved with NR==1 🙂
Kakowi Desu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
23