[Bash Shell Scripting] Update echoed lines?

For everything that's not in any way related to PureBasic. General chat etc...
codemaniac
Enthusiast
Enthusiast
Posts: 289
Joined: Mon Apr 02, 2007 7:22 am
Location: Finland

[Bash Shell Scripting] Update echoed lines?

Post 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!
Cute?
WishMaster
Enthusiast
Enthusiast
Posts: 277
Joined: Fri Jun 17, 2005 7:13 pm
Location: Franconia
Contact:

Post 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 
Image Image
codemaniac
Enthusiast
Enthusiast
Posts: 289
Joined: Mon Apr 02, 2007 7:22 am
Location: Finland

Post 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 :)
Cute?
naw
Enthusiast
Enthusiast
Posts: 573
Joined: Fri Apr 25, 2003 4:57 pm

Post 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} 
Ta - N
Post Reply