LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-24-2005, 02:29 AM   #1
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Rep: Reputation: 15
How to show a progress bar on the next line with shell script?


I want to show a progress bar with shell script, such as:
[======>.........]
I know that "tput cup $X $Y" could set the position for output, but I don't know how to get the current line number on screen to make the output been on the next line?

Is there any one can help me?

Thanks.

Last edited by Chowroc; 10-24-2005 at 03:00 AM.
 
Old 10-24-2005, 11:42 PM   #2
herbc
Member
 
Registered: May 2004
Location: United States
Distribution: Slackware 10.0
Posts: 72

Rep: Reputation: 15
Write a short program using ncurses and call getyx, and export the values to the environment? Then use your program in your script?

It's probably already been done somewhere.
 
Old 10-24-2005, 11:55 PM   #3
uberNUT69
Member
 
Registered: Jan 2005
Location: Tasmania
Distribution: Xen Debian Lenny/Sid
Posts: 578

Rep: Reputation: 30
Perhaps it would be an idea to look at the source for wget?
 
Old 10-25-2005, 01:15 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
Try something like so:

Code:
#!/bin/bash

for i in `seq 1 25`; do
    echo -n "["
    for j in `seq 1 $i`; do
        echo -n "="
    done
    echo -n ">"
    for j in `seq $i 24`; do
        echo -n "."
    done

    echo -n "]"

    sleep 1

    echo -en "\r"
done
You'll have to tailor it to your own needs, but it should show the basic idea.

echo -n tells the echo not to add a line feed, the -e allows interpretation of backslash escapes and \r is a carriage return (w/o the accompanying line feed).

Last edited by deiussum; 10-25-2005 at 01:17 AM.
 
Old 10-25-2005, 09:09 PM   #5
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
Thank you very much.
 
Old 10-25-2005, 11:58 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
I was putzing with that and I'm trying to figure out
where I could put $pct so it would continue to update.
Code:
#!/bin/bash
# Example: ./test original_file destination_file
usage()
{
   echo "Usage: $0 original_file destination_file"
   exit 1;
}

test $# == 2 || usage
orig_size=`stat -c %s $1`

>$2
dest_size=0
cp -f $1 $2 &

while [ $orig_size -gt $dest_size ] ; do
   dest_size=`stat -c %s $2`
   pct=$((( 100 * $dest_size ) / $orig_size ))
for i in `seq 1 69`; do
    echo -n "["
    for j in `seq 1 $i`; do
        echo -n "="
    done
    echo -n ">"
    for j in `seq $i 68`; do
        echo -n "."
    done
    echo -n "]"
    sleep 1
    echo -en "\r"
done
done
echo
 
Old 10-26-2005, 12:19 AM   #7
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
Something like so might work for you.

Code:
#!/bin/bash
# Example: ./test original_file destination_file
usage()
{
   echo "Usage: $0 original_file destination_file"
   exit 1;
}

test $# == 2 || usage
orig_size=`stat -c %s $1`

>$2
dest_size=0
cp -f $1 $2 &

while [ $orig_size -gt $dest_size ] ; do
   dest_size=`stat -c %s $2`
   pct=$((( 69 * $dest_size ) / $orig_size ))

    echo -en "\r["
    for j in `seq 1 $pct`; do
        echo -n "="
    done
    echo -n ">"
    for j in `seq $pct 68`; do
        echo -n "."
    done
    echo -n "] "
    echo -n $((( 100 * $pct ) / 69 ))
    echo -n "%"
done
echo

Last edited by deiussum; 10-26-2005 at 10:11 AM.
 
Old 10-26-2005, 09:32 AM   #8
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Thanks but that makes the cursor flash back and forth on the progress bar.
I was looking for something like this .....
Code:
[============>..................................] 45%
 
Old 10-26-2005, 09:46 AM   #9
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
Well, the \r causes the cursor to jump back to the beginning of the line and you overwrite everything. The cursor is going to flash back and forth somewhat.

I edited it a bit up there to do the \r at the beginning of the while loop instead of the end so it might limit it a bit. Also added the percent there for you.
 
Old 10-26-2005, 11:08 AM   #10
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Thanks, that's great!

Note to self: Don't try to program without sleep.
 
Old 10-26-2005, 10:27 PM   #11
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
But I still have the question: How to get the current line on screen?

Because I maybe want to show the status dynamically on serveral lines., and sometimes the prompt is not at the buttom of the scrren. Thus I must use some mechanism to change x,y of the cursor.

Still thank you.
 
  


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
PROBLEM: pygtk progress bar bendeco13 Programming 3 04-05-2005 02:54 PM
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


All times are GMT -5. The time now is 01:20 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration