4 Replies - 239 Views - Last Post: 23 April 2013 - 03:46 PM Rate Topic: -----

#1 JohnFYI  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-November 12

Update echo while running in console?

Posted 22 April 2013 - 11:47 AM

I have been doing a lot of command line scripts recently, I was wondering if its possible to update a echo with the new values of a variable. So for example.

I make it echo out.
"1 out of 10 done."
Then with its done 2 update it so it goes
"2 out of 10 done."
but all on the same echo so it doesn't just spam the screen with hundreds of echos.
Is This A Good Question/Topic? 0
  • +

Replies To: Update echo while running in console?

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Re: Update echo while running in console?

Posted 22 April 2013 - 12:07 PM

Well, I am not sure if Mac or Windows supports this, but Fedora Linux seems to, so I am going to give you this advice from that view.

You should be able to do what you are looking at accomplishing.

If you are wanting to use the same output statement for multiple things (like a progress bar) you could do something like so:
print("Progress:\n");
for($i = 0; $i <= 10; $i++){
    print("\r$i of 10");
    flush(); // force printing of the output buffer
    sleep(1); // sleep for a second so we can see what is going on
}
print("\n");


(code tested on Fedora 17)

In this case the \r is a return, which in unix (and maybe more) should tell the console to return to the beginning of the line (column 0).

Now, if you want instead to have something in the form of 10/#, for whatever reason, you could use the \b character (which is the backspace).

Hope that helps.

<edit>Fixed my inaccuracy</edit>
Was This Post Helpful? 1
  • +
  • -

#3 JohnFYI  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 19-November 12

Re: Update echo while running in console?

Posted 22 April 2013 - 12:13 PM

That helped me a lot thanks! Going to try and put it into my code now and see if it will work for me! Thanks man!

This post has been edited by Dormilich: 22 April 2013 - 10:24 PM

Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

Re: Update echo while running in console?

Posted 22 April 2013 - 07:37 PM

View PostBetaWar, on 22 April 2013 - 07:07 PM, said:

... however probably not with echo since it automatically adds a newline character. If you change over to print it should be easier.

That's not accurate. Neither the echo nor print constructs add a newline character to the output. They print only exactly what you feed them. They are essentially the same exact thing, except echo doesn't return anything and can take in multiple arguments.

I frequently use echo to do exactly the same thing you suggested: use \r to overwrite previous lines. - By the way, it works fine in both the Windows CMD and PowerShell programs, as well as in all the Linux terminals I've tried.
Was This Post Helpful? 1
  • +
  • -

#5 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,110
  • Joined: 07-September 06

Re: Update echo while running in console?

Posted 23 April 2013 - 03:46 PM

Ah, thanks for the correction. I guess I haven't really looked that deeply into echo. Always nice to learn something new!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1