Bash 每次循环更改时,让echo和grep进行更换n

Bash 每次循环更改时,让echo和grep进行更换n,bash,Bash,我的文本文件“testnum”如下所示: xxx1abc xxx2abc xxx3abc xxx4abc xxx5abc xxx6abc xxx7abc xxx8abc xxx9abc xxx10abc..etc 这将在cmdline中提供所需的输出 for n in {1..10} do o=`sed -n "${n}p" < testnum` clear echo -e "you read ${o}\r" grep -n -C 2 ${o} testnum sleep 2s do

我的文本文件“testnum”如下所示:

xxx1abc
xxx2abc
xxx3abc
xxx4abc
xxx5abc
xxx6abc
xxx7abc
xxx8abc
xxx9abc
xxx10abc..etc
这将在cmdline中提供所需的输出

for n in {1..10}
do
o=`sed -n "${n}p" < testnum`
clear
echo -e "you read ${o}\r"
grep  -n -C 2 ${o} testnum 
sleep 2s
done
只是它每次迭代时都会清除cmd行 我想要的是这个

nikola@nikola-desktop:~/Downloads$bash my_skirpt
you read xxx1abc
1:xxx1abc
2-xxx2abc
3-xxx3abc
最后一次迭代应该是

nikola@nikola-desktop:~/Downloads$bash my_skirpt
    you read xxx10abc
    8-xxx8abc
    9-xxx9abc
    10:xxx10abc
    11-xxx11abc
    12-xxx12abc
nikola@nikola-desktop:~/Downloads$

我不完全确定,但我想你希望它覆盖上一次迭代中打印的所有行吗?在这种情况下,您需要使用
tput

#!/bin/bash
tput sc
for n in {1..10}
do
o=`sed -n "${n}p" < testnum.txt`
echo -e "you read ${o}\r"
grep  -n -C 2 ${o} testnum.txt
sleep 2s
tput rc
tput el1
done;
tput sc
#/bin/bash
tput sc
对于{1..10}中的n
做
o=`sed-n“${n}p”
tput sc
保存当前光标位置,以便稍后使用
tput rc
调用此位置
tput el1
清除当前的行,以便不断变化的行数(第一个和最后一个输出比其他输出短)不会破坏任何内容


你可以用
tput
做更多的事情,例如,谷歌会给你一个页面,我觉得这很好。

我想让echo和grep在每次循环更改时都被替换,而不是一个新的输出n转换器,仍然无法解决。您想多次运行
my_skirpt
并使
n
前进吗?所以第一次运行程序时它返回第一行,第二次运行程序时它返回第二行,等等?我也很难理解你的意思。我想我理解了。您希望脚本不清除命令行,而是在每次迭代中“替换”实际文本。
#!/bin/bash
tput sc
for n in {1..10}
do
o=`sed -n "${n}p" < testnum.txt`
echo -e "you read ${o}\r"
grep  -n -C 2 ${o} testnum.txt
sleep 2s
tput rc
tput el1
done;
tput sc