linux - Linux bash:获取终端光标位置后的奇怪行为
问题描述
我编写了一个 shell 脚本来收集并在成功登录后显示一些信息。然而,有些信息需要一些时间来收集,所以我在一些标题和已经可用的信息之前打印到终端(ssh putty),然后再返回并将延迟的信息打印到正确的位置。
为了实现这一点,我使用以下脚本来获取当前光标位置,(忽略之前出现的所有无聊的东西。这是一堆 printf、cat 和 cut...
. ...
. ...
printf "^[[0m""\n"
# Get current settings.
if ! termios="$(stty -g 2>/dev/null)" ; then
echo "Not running in a terminal." >&2
exit 1
fi
# Restore terminal settings when the script exits.
trap "stty '$termios'" EXIT
# Disable ICANON ECHO. Should probably also disable CREAD.
stty -icanon -echo
# Request cursor coordinates
printf '\033[6n'
# Read response from standard input; note, it ends at R, not at newline
read -d "R" rowscols
# Clean up the rowscols (from \033[rows;cols -- the R at end was eaten)
rowscols="${rowscols//[^0-9;]/}"
rowscols=("${rowscols//;/ }")
#printf '(row %d, column %d)\n' ${rowscols[0]} ${rowscols[1]} *<-- commented by me*
# Reset original terminal settings.
stty "$termios"
# To the stuff...
printf '(row %d, column %d)\n' ${rowscols[0]} ${rowscols[1]}
line=${rowscols[0]}
line=$(($line - 10)) *<--- Indeed script's line 102. I want subtract 10*
col=56
printf '(r= %d, c= %d)\n' ${line} ${col} *<--- Printed two times, both times wrong values*
exit 1 *<--- Put here just to exit earlier*
## Get uptime/activetime formated to my taste.
m_activetime=$(/usr/bin/activetime -v)
printf "\33[%d;%dH^[[38;5;196m ${m_activetime}" ${line} ${col}
. ...
. ...
当我运行代码时,我得到:
. ...
. ...
. ...
||=-= _ |-=- |+++++++| _ ||= _ | :
`~‾‾ '--~~__|- = |+++++__|----~‾ ‾~`---', CPU stat⸱:
~---__|,--~' Weather⸱⸱:
(row 16, column 1)
./c.asc: line 102: 16 1 - 10: syntax error in expression (error token is "1 - 10")
(r= 16, c= 1)
(r= 56, c= 0)
lr@pi:~ $
1) 脚本是 bash (shebang #!/usr/bash
)
2)这条线(row 16, column 1)
似乎没问题!
3) 脚本名为 c.asc
4)我想知道那个错误到底是什么,我以前使用过类似的表达式,不是使用 bash 数组,但即便如此......
line 102: 16 1 - 10: syntax error
我可以猜到 16,但它是从哪里来的1 - 10
?
(error token is "1 - 10")
什么令牌“1 - 10”????!!!
5)第一个(r= 16, c= 1)
已经错了,应该是(r= 6, c= 56)
。为什么是这样?10的减法是怎么回事?变量col的值去哪儿了?
6)更奇怪。我没有指示第二次打印,即便如此,现在变量line存在身份危机并显示col值,并且在这两种情况下,指令 col=56 似乎都被忽略了。为什么以及如何变量line获得变量col的值?为什么变量col从错误的值 1 变为错误的值 0?
7) 显示的脚本已被转换以跟踪错误。它首先没有打印到预期的位置,并显示错误。还有一个版本的 printfprintf '(r= %d, c= %d)\n' $((${line} - 10)) ${col}
显示同样相似和离奇的错误。
ps
经过一些额外的实验,只用脚本的一部分来获取终端光标位置,它似乎也不完全正常。它可以返回位置,但尝试类似read r c < <(curspos)
, (假设curspos是返回元组的脚本的名称lin col
),提示会挂起,直到按下 Ctrl-C 并且在该提示变得疯狂之后。
谢谢
解决方案
问题是您将值引用到数组中。
rowscols=("${rowscols//;/ }")
这告诉 bash 忽略空格并将其视为一个值。因此,当您稍后获得第一个值时${rowscols[0]}
,您实际上得到的是16 1
而不是16
并且没有第二个值。
它也适用于此 printf ,因为您没有引用那里的值。
printf '(row %d, column %d)\n' ${rowscols[0]} ${rowscols[1]}
我不知道为什么它运行了最后一次 printf 两次,但它似乎通过引用解决了。
推荐阅读
- string - readStartDocument 只能在 CurrentBSONType 为 DOCUMENT 时调用,不能在 CurrentBSONType 为 STRING 时调用 错误?
- parallel-processing - 如何在单机上使用 cypress 并行运行具有不同数据集的单个测试
- bash - 从具有变量扩展的 bash 脚本运行时的 jq 格式
- asp.net - 如何将 Visual Studio 2010 Web 应用程序从 Http 转换为 Https
- python - 从值列表生成简单的直方图
- flutter - Dart uuid 无法扩展
- bioinformatics - 需要命令或脚本使用模式匹配重命名 linux 中的文件列表
- javascript - 无法使用 send_data 或 send_file 将数据下载为文件
- postgresql - Postgres 的 GCC/Clang 中的锁争用很高
- php - SyslogHandler + JSON 日志导致 Google Cloud Logging 中的格式损坏