1

I have a bash script which get details from many servers. 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.

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

@cbuckley:

Thanks for your answer, but its not working correctly. In this way with -ne i tryed it already. 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. (i think this should normaly work with -ne, but in my case it dont).

4
  • Please show your expected output - it's difficult to tell what you want without knowing the contents of your variables and an example result. Commented Nov 11, 2013 at 19:54
  • It's not clear what you are calling a line. Do you have a file that you want to update in place? Commented Nov 11, 2013 at 20:09
  • Please see my "should be" example in my first post - this is what it should like to be. The values under the IP should get constantly updated. Currently, the updated get written into new lines. Old values should get replaced with new values. Commented Nov 11, 2013 at 20:09
  • @chepner No, the output is in shell. No file. Commented Nov 11, 2013 at 20:10

2 Answers 2

1

If you've already outputted across multiple lines, you can't remove those lines without clearing the screen. You have two options:

Using watch

You can write a script that outputs the stats once, and then use watch to repeatedly that script:

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer, but its not working correctly. In this way with -ne i tryed it already. Result is: 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. (i think this should normaly work with -ne, but in my case it dont). edit: comments dont keep formating. i add it into my first post.
Thanks. i done it already with clear but its not a option for me. So Bash can not directly update a line? I will try to write the output into a file, edit new values in file with sed -i (like mti2935 sayed) and cat the file for output. I think this should work good, do you have experience with this? Could this work? And thank you for your fast answers!!
I checked again watch and its exactly what i searched. Thanks so much! Would be great if you could answer my question about sed -i too (I'm interested if it would work)
sed -i is only useful for in-place editing of a file that exists on disk. It doesn't really have any use here. As with above, you can't modify what's been output to the screen after you've done it, unless you clear the screen first.
0

You might want to try using sed with the -i option to edit a file 'in place' (i.e. to change the existing file instead of writing a new file)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.