I am having an issue with the WINCH signal in the following code which is from a tutorial located at developerWorks®:
#!/bin/bash
trap 'get_window_size' WINCH # trap when a user has resized the window
_UNDERLINE_ON=`tput smul` # turn on underline
_UNDERLINE_OFF=`tput rmul` # turn off underline
get_window_size() {
_WINDOW_X=`tput lines`
_WINDOW_Y=`tput cols`
_FULL_SPACES=`echo ""|awk '
{
_SPACES = '${_WINDOW_Y}'
while (_SPACES-- > 0) printf (" ")
}'`
_FULL_UNDERLINE=`echo "${_UNDERLINE_ON}${_FULL_SPACES}${_UNDERLINE_OFF}"`
unset _FULL_SPACES
show_menu
return 0
}
set_color() {
tput clear
PS3="Enter Selection[1-9]:"
select _COLOR in "Black" "Blue" "Green" "Cyan" "Red" "Magenta" "Yellow" "White" "Exit"
do
case ${REPLY} in
[1-8]) _X=`expr ${REPLY} - 1`;;
9) break;;
*) echo "Invalid Color"; continue;;
esac
if [[ ${1} = "b" ]]
then
tput setb ${_X}
else
tput setf ${_X}
fi
done
}
show_menu() {
while [[ -z ${_ANS} ]]
do
tput civis
tput clear
cat <<- EOF
Window Size: ${_WINDOW_X} / ${_WINDOW_Y}
Select => ${_UNDERLINE_ON} ${_UNDERLINE_OFF}
${_FULL_UNDERLINE}
B) Background Text Color
F) Foreground Text Color
X) Exit
EOF
tput rc
tput smul
tput cnorm
read _ANS
tput rmul
case ${_ANS} in
[Bb]) set_color "b";;
[Ff]) set_color "f";;
[Xx]) tput clear; exit;;
*)
echo -e "Invalid Selection: ${_ANS}\c"
sleep 2
;;
esac
unset _ANS
done
}
tput sgr0
tput civis
tput clear
tput cup 3 10
tput sc
tput cup 0 0
[[ -n ${_ANS} ]] && unset _ANS
get_window_size
exit 0
Since I am trapping the WINCH (window change) signal, I expect that (each time I adjust the window which contains this running script) the get_window_size function will be invoked.
However, on my machine, it only traps the WINCH signal once (under specific conditions). The trap happens only if I use the xterm (in this case, gnome-terminal) Terminal menu to choose one of the default sizes (80x24, 80x43, 132x24, 132x43); further, it happens only the first such time. If I subsequently change the window size (using the Terminal menu item), no trapping occurs.
Also, if I don't use the Terminal menu item, but re-size using a pointing device, even the first trap does not happen.
Can someone help me to understand what is wrong (either with the code or with my expectation)?