Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Systems > Linux / BSD / OS X

Reply
 
LinkBack Thread Tools Display Modes
Old 08-15-2004, 12:06 PM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
writting progress to cl on one line

I use php4-cgi to write scripts on my linux box. The scripts are going through lots of data and I want to print output to the screen as it runs.

Printing output to the screen is no problem, but it prints out line for line, and soon enough your screen is full of lines.

I've seen a perl program that keeps all the output on 1 line. it updates the same line till it is complete.

so, even though it prints a new value for each % it is complete, here is how a program calld "someprogram.pl" would look after running it:
Code:
$ ./someprogram.pl
[ 50%] Complete
does that make sense? normally it would read
Code:
$ ./someprogram.pl
[  1%] Complete
[  2%] Complete
[  3%] Complete
.. and so on
Any ideas how to keep output to 1 line?
__________________
Mike
sde is offline   Reply With Quote
Old 10-27-2004, 01:50 PM   #2 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
Thumbs up Carriage Return

This might be a little late, and you might have figured it out already. What you are looking for is the Carriage Return character. This can be accomplished by printing the "\r" special character. This resets the cursor to the beginning of the same line you are currently on. I recently wrote a colored progress bar for the terminal in perl this way.

This makes windows version of newlines a lot easier to understand. Windows requires the full \n\r to get to the next line. I assume it is because they don't automatically reset the cursor to the beginning of the line when the cursor moves down. Therefore, you need the \r in there. Linux does this automatically, and so we get fun battles where lines don't look right in windows when they looked fine in linux.

Although you probably knew that already, so I'm gonna stop now. However, before I sign off, I will mention that if you are looking for cool stuff to do to a 'standard' vt200 terminal (which most linux terminals emulate anyway) the following link will help: Terminal Escape Sequences

Hope this helps. Good luck!

- Ted
ender is offline   Reply With Quote
Old 10-27-2004, 01:53 PM   #3 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
Thanks Ted .. nice to see you here again. I did not know that, I actually gave up and just settled with the multi-line output. This gives me something to play with. I appreciate it!
__________________
Mike
sde is offline   Reply With Quote
Old 11-08-2004, 09:02 AM   #4 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,475
sde is on a distinguished road
well to bring this up again, is there anyway to do it with bash scripting? i can get it to work if i use php or perl, .. but not if i write a simple bash script.
__________________
Mike
sde is offline   Reply With Quote
Old 11-08-2004, 01:15 PM   #5 (permalink)
ender
Code Monkey
 
ender's Avatar
 
Join Date: Mar 2003
Location: Evansville, IN
Posts: 75
ender is on a distinguished road
Send a message via AIM to ender Send a message via Yahoo to ender
You should be able to do it in bash. Although determining the percentage to fill might be a little trickier than I would like it to be. The overall concept is the same though. I'm sure you knew this, but be sure to do 'echo -e' to enable interpretation of escape sequences. I think this may be dependent upon which shell you use. However I do know that /bin/echo is pretty much a constant.

The way you can do your percentages if you want to is to is pipe any calculation you want into bc and read the answer. For example, finding 2+2 : 'echo "2+2" | bc" will result in 4 on stdout.

The following should work for just a percentage though:

Code:
#!/bin/bash

echo "Start"

for i in 1 2 3 4
do
  percentage=`echo "scale=2; $i/4 * 100" | bc -l`
  echo -en "\r$percentage %"
  sleep 1
done

echo -e "\nDone!"
This should get you started. Oh, the -n option for echo suppresses the newline from being printed after the echo 'echos' something. Good Luck!

-Ted
__________________
while(1) fork();
ender is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help with my article/blog script I have been writting eugene1 PHP 2 05-29-2003 05:37 PM
Silly Question: Adding a line break in a string JeC PHP 4 03-17-2003 10:06 AM
cgi script Henry PHP 25 12-30-2002 06:54 AM
so where exactly is the line drawn for ... sde PHP 2 06-05-2002 09:18 AM


All times are GMT -8. The time now is 10:59 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting