Page 1 of 1

[Bash Shell Scripting] Update echoed lines?

Posted: Mon Jul 02, 2007 9:50 pm
by codemaniac
Hello!

I am stuck in my Bash script again, this time I need to do something complex; I want to echo a line and then after 1 second "modify" the previous line to something else.

Here's a GIF of what I want to do (I did it in GIMP):
Image
(Ofcourse I don't want it to loop, but I want to increment the number in the end of the last line and update the string after 1 second.)

A simple approach would be to clear the screen and print everything again, but that's too lame and a little slow if there's lots of content to clear and echo again.. Can anyone help me? Thanks!

Posted: Mon Jul 02, 2007 10:58 pm
by WishMaster
VoilĂ :

Code: Select all

echo -en "Please wait, i'll be overwritten soon"\\\r; sleep 2; echo -en "Hello, I'm the new text... (oh, and I must be longer than the one before or have some spaces at the end)" \\n 

Posted: Tue Jul 03, 2007 2:19 pm
by codemaniac
Here's a solution to avoid the need of having the replaced string to be longer:

Code: Select all

START_STRING="Test string..."
END_STRING="Want tea?"
WAIT_TIME=2
echo -en "$START_STRING\r"; sleep $WAIT_TIME; echo -en "$END_STRING"; for i in $(seq 1 $(echo -en "$START_STRING" | wc -m)); do echo -en " "; done; echo -en "\n"
It needs to be more polished, but atleast it works for me :)

Posted: Wed Jul 04, 2007 1:19 am
by naw
echo is not supported on all shells (often aliased to print command) but should be the same on all bash shells.
I suggest you use printf command instead. printf is very useful (more powerful) and is implemented the same way - well worth reading up on.
Would be very nice if PB supported the powerful printformatting conventions that printf uses:

Code: Select all

while true
do
printf "${ctr}\r"
let ctr=${ctr}+1
done
or replace

Code: Select all

printf "${ctr}/r"
with

Code: Select all

printf "%5d\r" ${ctr}
or

Code: Select all

printf "%05d\r" ${ctr}