Can someone explain to me what's going on here?
Script:
#!/bin/sh
SKIP="unity-launcher|unity-panel|unity-dash|Hud|XdndCollectionWindowImp|Desktop"
WINS=()
wmctrl -l | grep -Ev " (${SKIP})" | cut -d \ -f 1 | while read window; do
WINS=( ${WINS[@]} $window )
echo "Found window: $window; New size: ${#WINS[@]}"
done
echo "Total window count: ${#WINS[@]}"
echo "Window IDs:"
for i in "${WINS[@]}"; do
echo " $i"
done
Output:
Found window: 0x0380000a; New size: 1
Found window: 0x038002ae; New size: 2
Found window: 0x03800a33; New size: 3
Found window: 0x03000001; New size: 4
Found window: 0x04c00004; New size: 5
Total window count: 0
Window IDs:
Expected:
Found window: 0x0380000a; New size: 1
Found window: 0x038002ae; New size: 2
Found window: 0x03800a33; New size: 3
Found window: 0x03000001; New size: 4
Found window: 0x04c00004; New size: 5
Total window count: 5
Window IDs:
0x0380000a
0x038002ae
0x03800a33
0x03000001
0x04c00004
Essentially, at the end of the while loop, the WINS array is getting reset somehow, and I have no idea why. Is there some weird scoping thing in bash I'm unaware of?
/bin/shcode; maybe instead use/usr/bin/env bashto find that shell, if it is installed?/bin/shdoes not have arrays. Use/bin/bashzshsyntax thanbashsyntax. That script would work with#! /bin/zsh -or#! /bin/ksh93 -as the she-bang.