LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-11-2004, 08:08 AM   #1
rooch84
Member
 
Registered: Aug 2004
Posts: 33

Rep: Reputation: 15
Shell Script Progress bar


I have a script thats copies 600 Megs of data from a CD to the hard disk. I wan to have a progress bar so show how far through the installation I am.

I would like to have it in the format:

[##### ]

Or an incrementing parentage.

Is this possible? If not would it at least be possible to have incrementing full stops until the copy is complete?

Thanks

Chris
 
Old 08-11-2004, 08:46 AM   #2
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31
Of course you'll need to use a counter of some kind, which is incremented, thus signifying the progress. Incrementing in bash can be written for instance like
Code:
COUNTER=$(($COUNTER + 1))
To calculate percentages you might find the command bc useful, which is "An arbitrary precision calculator language", according to the man page To compute something you can use the notation
Code:
echo "scale=3; 100*$COUNTER/$NSTEPS" | bc -l
To increment something on the same line, use the echo in combination with the -n option. Furthermore, I find \b useful in these contexts, which is a backspace. Try it out for yourself with stuff like
Code:
echo -en "hello\b"
The -e option tells echo to interpret the escaped expressions, such as \b and \n.

Another way to delete the progress indicator's current value is to use tput. tput sc will tell tput to remember ("save") the current position of the cursor. tput rc will "retreive" it. tput el or tput el1 will do the actual deletion.

Hope this gives you some ideas on how you can do it.

Last edited by Bebo; 09-03-2005 at 07:43 PM.
 
Old 08-11-2004, 09:14 AM   #3
rooch84
Member
 
Registered: Aug 2004
Posts: 33

Original Poster
Rep: Reputation: 15
That has given me a good start.

There is still one thing that I don't know how to do:

Make it run only whilst the cp command is running.
 
Old 08-11-2004, 09:30 AM   #4
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31
That is a good question. One method, however not very beautiful, is to copy the files one by one in a loop, along the lines
Code:
find $BASEDIR -type f | while read FILE ; do
   cp $FILE $DESTDIR
done
Doing it exactly like that would destroy the directory structure under $BASEDIR completely, but that problem could be overcome. It might be very cumbersome, though.

Another method, which I don't know very much about, since I haven't written any of those scripts yet, is to put the copying in the background with the ampersand (&), and then let the script check with regular intervals if that particular process is still running. If the process is still there, then increment the counter and then pause for some time, perhaps one second. I think that the special variable $! contains the pid of the latest background process. So maybe like this:
Code:
cp -R $DIR $DESTDIR &

while [ $(ps -p $! | grep cp) -gt 0 ] ; do
   COUNTER=$(($COUNTER + 1))
   ...blah...
done
The problem here is that one has to know how long time the copying will take, or one has to count the copied files. In the latter case, we're basically back to the first suggestion, i.e. copying one file at a time.

Cheers

Last edited by Bebo; 08-11-2004 at 09:31 AM.
 
Old 08-11-2004, 09:38 AM   #5
rooch84
Member
 
Registered: Aug 2004
Posts: 33

Original Poster
Rep: Reputation: 15
I'll look into the latter.

I think I'll leave it at just outputting '.....' until the copying as finished.

Thanks

Chris
 
Old 08-11-2004, 10:03 AM   #6
rooch84
Member
 
Registered: Aug 2004
Posts: 33

Original Poster
Rep: Reputation: 15
Final code:

cp -r $DIR $DESTDIR &

ps -A | grep cp -> /tmp/cp_process

while [ -s /tmp/cp_process ]
do
COUNTER=$(($COUNTER + 1))
echo -n .
ps -A | grep cp -> /tmp/cp_process
sleep 2
done


Not the best, but I've only been scripting for 3 days.

Thanks for your help guys

Chris
 
Old 08-11-2004, 10:04 AM   #7
rooch84
Member
 
Registered: Aug 2004
Posts: 33

Original Poster
Rep: Reputation: 15
just realised that I don't need the COUNTER line
 
Old 08-11-2004, 10:06 AM   #8
Bebo
Member
 
Registered: Jul 2003
Location: Göteborg
Distribution: Arch Linux (current)
Posts: 553

Rep: Reputation: 31
Yep, that would be unnecessary if you only want to see that something is happening
 
Old 09-03-2005, 04:55 PM   #9
spurious
Member
 
Registered: Apr 2003
Location: Vancouver, BC
Distribution: Slackware, Ubuntu
Posts: 558

Rep: Reputation: 31
I stumbled across a utility called Pipe Viewer (pv) which is essentially a progress viewer for the console. To install in Debian, all you have to do is 'apt-get install pv'

From the man page:

pv allows a user to see the progress of data through a pipeline, by giving information such as time elapsed, percentage completed (with progress bar), current throughput rate, total data transferred, and ETA.

To use it, insert it in a pipeline between two processes, with the appropriate options. Its standard input will be passed through to its standard output and progress will be shown on standard error.

pv will copy each supplied FILE in turn to standard output (- means standard input), or if no FILEs are specified just standard input is copied. This is the same behaviour as cat(1).

A simple example to watch how quickly a file is transferred using nc(1):

pv file | nc -w 1 somewhere.com 3000

A similar example, transferring a file from another process and passing the expected size to pv:

cat file | pv -s 12345 | nc -w 1 somewhere.com 3000

A more complicated example using numeric output to feed into the dialog(1) program for a full-screen progress display:

(tar cf - . \
| pv -n -s `du -sb . | awk '{print $1}'` \
| gzip -9 > out.tgz) 2>&1 \
| dialog --gauge 'Progress' 7 70
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cp with progress bar? Rotwang Linux - General 5 04-27-2005 07:49 PM
PROGRESS BAR with shell script murugesan Linux - Software 5 12-13-2004 09:03 AM
Progress bar image craigs1987 Fedora 0 05-27-2004 02:31 PM
cp: progress bar chii-chan Linux - General 2 10-30-2003 06:30 PM
Progress Bar zael Programming 3 10-01-2003 12:20 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration