Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to implement a progress report

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

I would like to be able to do two things in a Cshell:
1) a progress report: for instance if I am reading x number of files or lines I would like to display something like:

Reading x line...

BUT I want to REPLACE the x with a new number on the same line not on the next line, is this possible? (using some king of backspace command maybe?)

2) I would like to display the following characters while a script is running
\ | / | BUT at the same location one after the other, is it possible?

Thanks for your help,

Fabien
 
Fabien,

I've not done this in the C shell, but the techniques I use for Perl shell programming may be applicable to you.

for each line in the file that I process, I run the following code:

if(($. % 1000) == 0){ # if the current line number
# is divisible by 1000
print "$. lines processed\r";
}

the \r character in the print line is a carraige return - it moves the cursor back to the beginning of the current line.

To do your rotating propellor thing you'll need to have code that checks which char you printed last time and then print the next one. Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Thanks Mike,

I have tried it but print "jdksjdsk/r" or echo don't seem to work in my shell...

Fabien
 
Look at the man page for tput which allows you to output a line at a specific cursor position.

Greg.
 
yep - that'll do it, too much Perl - that's my problem Mike
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Fabien,

Here is a sample using tput. Definitely do a man lookup on terminfo as well as tput. `tput ri` moves back one line and `tput ind` moves forward one line. There are endless ways to use tput for your problem. Other ways would such choices as clear, cup <row> <col>, civis, el1, el, ed just to name a few. You'll find them in terminfo. I'll let you make the changes from Bourne to C.

#! /bin/sh
count = 11
idx = 1

while [ &quot;$IDX&quot; -lt &quot;$COUNT&quot; ]; do
echo &quot;Reading ${IDX} line...&quot;
sleep 1
tput ri
IDX=$(($IDX + 1))
done

tput ind
echo &quot;Done&quot;
# END SCRIPT

Cheers,
ND
 
The following example creates a 'propellor':
Code:
#!/usr/bin/csh
set progress = &quot;/&quot;
while (1)
    printf &quot;$progress \r&quot;
    switch (&quot;$progress&quot;)
        case &quot;/&quot;:
            set progress = &quot;-&quot;; breaksw;
        case &quot;-&quot;:
            set progress = &quot;\&quot;; breaksw;
        case &quot;\\&quot;:
            set progress = &quot;|&quot;; breaksw;
        case &quot;|&quot;:
            set progress = &quot;/&quot;; breaksw;
    endsw
    sleep 1
end
Start your long process in the background, then break out of the while loop when the process has finished.
Cheers, Neil :)
 
Or another way:
Code:
set i = 0
set index = 1
set char = ('-' '\\' '|' '/')
while ( $i < 30 )
    echo &quot;$char[$index]\b\c&quot;
    if ($index == $#char) then
        set index = 0
    else
        @ index = $index + 1
    endif
    @ i = $i + 1
    sleep 1
end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top