Dar*_*ous 3 shell bash terminal
bash 有没有办法从屏幕上的 xy 坐标读取字符?这个命令类似于
cget 12 30
Run Code Online (Sandbox Code Playgroud)
这将返回第 12 行第 30 列的字符。
如果您在文本模式下使用控制台 ttys(/dev/tty1
通过/dev/tty7
),您可以直接从相应的/dev/vcsN
设备读取屏幕缓冲区。
由于您不应该真的假设每行有 80 个字符,因此有必要让终端显示每行的字符。然后简单的数学运算将 (x,y) 坐标转换为偏移量 (y*c + x) 将为您提供所需的字符:
#!/bin/bash
#
my_tty=$(tty)
vcs_nr="${my_tty/*tty/}"
# Read Y, X from first two characters of /dev/vcsaN (we only use X)
xwidth=$(
dd if="/dev/vcsa$vcs_nr" bs=1c count=2 2>/dev/null |
od -t u1 -A d |
awk '{print $3; exit}'
)
# Calculate byte offset into the screen
offset=$(( ($2 -1) * xwidth + ($1 -1) ))
# Read the data
dd count=1 skip="$offset" bs=1 if="/dev/vcs$vcs_nr" 2>/dev/null
Run Code Online (Sandbox Code Playgroud)
当然,这预设了对 /dev/vcsN 设备的 root 访问权限。