加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在bash中获取光标位置?

发布时间:2020-12-15 19:12:06 所属栏目:安全 来源:网络整理
导读:在一个bash脚本中,我想将光标列放在一个变量中。看起来像使用ANSI转义码{ESC} [6n是获取它的唯一方法,例如以下方式: # Query the cursor positionecho -en '33[6n'# Read it to a variableread -d R CURCOL# Extract the column from the variableCURCO
在一个bash脚本中,我想将光标列放在一个变量中。看起来像使用ANSI转义码{ESC} [6n是获取它的唯一方法,例如以下方式:
# Query the cursor position
echo -en '33[6n'

# Read it to a variable
read -d R CURCOL

# Extract the column from the variable
CURCOL="${CURCOL##*;}"

# We have the column in the variable
echo $CURCOL

不幸的是,这将打印字符到标准输出,我想静静地做。此外,这不是很便携…

有没有一个纯粹的bash方式来实现这一点?

你必须诉诸肮脏的技巧:
#!/bin/bash
# based on a script from http://invisible-island.net/xterm/xterm.faq.html
exec < /dev/tty
oldstty=$(stty -g)
stty raw -echo min 0
# on my system,the following line can be replaced by the line below it
echo -en "33[6n" > /dev/tty
# tput u7 > /dev/tty    # when TERM=xterm (and relatives)
IFS=';' read -r -d R -a pos
stty $oldstty
# change from one-based to zero based so they work with: tput cup $row $col
row=$((${pos[0]:2} - 1))    # strip off the esc-[
col=$((${pos[1]} - 1))

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读