成功登录后,我编写了一个Shell脚本来收集并显示一些信息。但是,一些信息需要花费一些时间来收集,因此我将打印一些标题和已经可用的信息到终端(ssh腻子),然后再将延迟的信息打印到正确的位置。
为此,我使用以下脚本来获取当前光标位置,(忽略之前出现的所有无聊内容。这是一堆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)更奇怪。即便如此,我也没有指示再次打印,但现在可变行出现了身份危机并显示了col值,在两种情况下,似乎都忽略了col = 56指令。可变行为什么以及如何获得变量col的值?为什么变量col从错误的值1转移到错误的值0?
7)显示的脚本已转换为跟踪错误。首先无法打印到预期位置,并显示错误。同样,printf的一个版本printf '(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两次,但似乎可以通过引用解决。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句