简体   繁体   English

Bash-替换并写入同一行

[英]Bash - replace & write into same line

I have a bash script which get details from many servers. 我有一个bash脚本,可以从许多服务器获取详细信息。 It works good but i want that the lines get updated and not get written new. 它工作正常,但我希望这些行得到更新而不是重新编写。

while [ true ] ; do
for i in $(seq ${#details[@]}); do
    .... more code (irrelevant)

echo   ${server[$i]}
echo   $stat1
echo   $stat2
echo   $stat3
echo   $stat4


done
done

How can i do, that all lines get constantly updated into same line? 我该怎么做,所有行都不断更新为同一行? I try with echo -ne but this makes that everything is in one long line. 我尝试使用echo -ne,但这使所有内容都排成一行。

I want that the line keep the place and just get updated with new value. 我希望该行保留位置,并仅使用新值进行更新。

Would be great if somebody knows a trick. 如果有人知道一个把戏,那就太好了。

Thank you! 谢谢!

UPDATE 1 更新1

@cbuckley: @cbuckley:

Thanks for your answer, but its not working correctly. 感谢您的回答,但无法正常工作。 In this way with -ne i tryed it already. 用-ne这样,我已经尝试过了。 Result is (it always create new lines): 结果是(它总是创建新行):

10.0.0.2
100310.0.0.1
72710.0.0.3
368310.0.0.2
100310.0.0.1
72710.0.0.3

Should be 应该

10.0.0.1
17
1003
10.0.0.2
319
727
10.0.0.3
157
3683

values under IP should get updated constantly. IP下的值应不断更新。 (i think this should normaly work with -ne, but in my case it dont). (我认为这通常应与-ne一起使用,但就我而言,它不起作用)。

If you've already outputted across multiple lines, you can't remove those lines without clearing the screen. 如果您已经跨多行输出,则必须先清除屏幕,才能删除这些行。 You have two options: 您有两种选择:

Using watch 使用watch

You can write a script that outputs the stats once, and then use watch to repeatedly that script: 您可以编写一次输出统计信息的脚本,然后使用watch重复该脚本:

watch -n 10 ./script.sh # calls script every 10 seconds.

Clearing the screen 清除画面

If that is not suitable, you'll need to clear the screen yourself: 如果不合适,则需要自己清除屏幕:

while [ true ] ; do
    clear # clear the screen

    for i in $(seq ${#details[@]}); do
        # ...
    done

    sleep 10 # don't update the screen too often
done

However, at this point, you've pretty much implemented a basic version of watch anyway. 但是,到目前为止,您已经实现了watch的基本版本。

您可能想尝试使用带有-i选项的sed来“就地”编辑文件(即更改现有文件而不是编写新文件)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM