Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
LinuxBash:获取终端光标位置后的奇怪行为_Linux_Bash_Terminal_Cursor - Fatal编程技术网

LinuxBash:获取终端光标位置后的奇怪行为

LinuxBash:获取终端光标位置后的奇怪行为,linux,bash,terminal,cursor,Linux,Bash,Terminal,Cursor,我编写了一个shell脚本,在成功登录后收集并显示一些信息。 然而,收集一些信息需要一些时间,所以我在返回并将延迟的信息打印到正确的位置之前,先向终端(ssh putty)打印一些标题和已经可用的信息 为了实现这一点,我使用了下面的脚本来获取当前光标的位置,(忽略了之前出现的所有无聊的东西。它是一堆printf、cat和cut的 . ... . ... printf "^[[0m""\n" # Get current settings. if ! termios="$(stty -g 2>

我编写了一个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}
. ...
. ...
1) 脚本是bash(shebang
#!/usr/bash

2) 行
(第16行,第1列)
似乎正常

3) 该脚本称为c.asc

4) 我想知道这个错误到底是什么,我以前使用过类似的表达式,不是bash数组,但即使如此

第102行:16 1-10:语法错误
我能猜到16,但它是从哪里来的
1-10

(错误标记为“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}
的一个版本同样显示类似和奇怪的错误


p、 美国


在对脚本的一部分进行了一些额外的实验以获得终端光标的位置之后,它似乎也不是完全正常的。它返回正确的位置,但是尝试像
readrc<这样的操作问题是您正在向数组引用值

rowscols=("${rowscols//;/ }")

这告诉BASH忽略空间并将其视为一个值。因此,当您稍后使用

${rowscols[0]}
获得第一个值时,实际上得到的是
16 1
而不是
16
,并且没有第二个值

它也适用于这个printf,因为您没有引用那里的值

printf '(row %d, column %d)\n' ${rowscols[0]} ${rowscols[1]}
我不知道为什么它运行了最后一个printf两次,但它似乎通过引用得到了解决。

Pynex先生的解决方案(如下)奏效了。无重复打印,为行和列计算的值正确。因此,詹姆斯·R.先生是正确的,“引用值”会导致重大的不可预测的问题。我从互联网上获取了光标位置的代码,无论我对stty文档做了多少研究,我都不理解其中的特定片段。但是,该代码不能用作可调用脚本,我不能使用
readlc<
printf '(row %d, column %d)\n' ${rowscols[0]} ${rowscols[1]}