获取终端下光标的位置

1 #!/bin/bash
2 #based on a script from http://invisible-island.net/xterm/xterm.faq.html
3 exec < /dev/tty
4 oldstty=$(stty -g)
5 stty raw -echo min 0
6 # on my system, the following line can be replaced by the line below it
7 echo -en "\033[6n" > /dev/tty
8 # tput u7 > /dev/tty # when TERM=xterm (and relatives)
9 IFS=';' read -r -d R -a pos
10 stty $oldstty
11 # change from one-based to zero based so they work with: tput cup $row $col
12 row=$((${pos[0]:2} - 1)) # strip off the esc-[
13 col=$((${pos[1]} - 1))
14
15 echo "(row,col): $row,$col"

posted @ 2014-05-20 22:09  ggaaooppeenngg  阅读(506)  评论(0编辑  收藏  举报