누군가 나에게 무슨 일이 일어나고 있는지 설명해 줄 수 있습니까?
스크립트:
#!/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
산출:
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:
예상되는:
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
본질적으로 루프가 끝나면 배열이 어떻게든 재설정 while
되는데 WINS
그 이유는 모르겠습니다. bash에 내가 모르는 이상한 범위 지정이 있나요?
답변1
파이프는 그 위의 프로세스에서 값을 변경할 수 없는 하위 쉘을 생성합니다. 노력하다:
while read window; do
WINS=( ${WINS[@]} $window )
echo "Found window: $window; New size: ${#WINS[@]}"
done < <(wmctrl -l | grep -Ev " (${SKIP})" | cut -d \ -f 1)