端末サイズ取得サンプルスクリプト(bash)
- #!/bin/bash
- die() {
- echo "$@"
- exit 1
- }
- printf "\e[18t"
- read -r -t 1 -n 3 resp
- [ $? -ne 0 ] && die "No response"
- CSI=$(printf "\x9b")
- ESC=$(printf "\e")
- case $resp in
- ${CSI}8\;) : ;;
- ${ESC}\[8)
- read -r -t 1 -n 1 resp
- [ $? -ne 0 -o "$resp" != ";" ] && die "Invalid Response";;
- *) echo "Invalid response";;
- esac
- state=0
- cols=0
- rows=0
- while [ $state -lt 3 ]; do
- read -r -t 1 -n 1 resp
- [ $? -ne 0 ] && die "Invalid Response"
- case $resp in
- [0-9])
- if [ $state -eq 0 ]; then
- rows=$((rows * 10 + resp))
- elif [ $state -eq 1 ]; then
- cols=$((cols * 10 + resp))
- fi;;
- \;) [ $state -lt 2 ] && state=$((++state));;
- t) state=3;;
- *) die "Invalid response";;
- esac
- done
- echo "COLUMNS=$cols"
- echo "ROWS=$rows"