LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices


Reply
  Search this Thread
Old 03-27-2009, 04:53 PM   #1
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Rep: Reputation: 73
Running percentage in bash for backups


Hello, I got a script which does backup for my databases, also I create a percentage to see how much is done.
The problem is that right now, the idea work line by line, meaning show the percent done.

Example:

Backup done: 77%
Backup done: 78%
Backup done: 80%
Backup done: 81%
Backup done: 82%
Backup done: 84%
Backup done: 85%
Backup done: 87%
Backup done: 88%
Backup done: 90%
Backup done: 91%
Backup done: 92%
Backup done: 94%
Backup done: 95%
Backup done: 97%
Backup done: 98%
Backup done: 100%

Does anyone know how to make this in one line, but without using clear, just like perl would do or C/C++?

Please let me know, im interested for bash or sh.

Thanks!
 
Old 03-27-2009, 06:17 PM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
Code:
i=1
while ((i<=100));do 
  printf '\r%d%% done' $i
  sleep 1
  ((++i))
done
For old, pre-POSIX shells:

Code:
i=1
while [ $i -le 100 ];do 
  printf '\r%d%% done' $i
  sleep 1
  i=`expr $i + 1`
done
 
Old 03-28-2009, 04:31 AM   #3
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Hello, it works but it doesnt, I cant added a text in front of the percentage and any line that i add it will be after the percentage.

Simple example:

#!/bin/sh

i=1
printf 'Doing stuff: '
while ((i<=100));do
printf '\r%d%% done' $i
sleep 1
((++i))
done

Output:
3% donetuff:

If you know how to fix this, please let me know.
 
Old 03-28-2009, 05:23 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Replace printf 'Doing stuff: ' by:

printf 'Doing stuff:\n'

or by

echo 'Doing stuff:'

Hope this helps.
 
Old 03-28-2009, 08:17 AM   #5
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
printf 'Doing stuff:\n' is seems to work, but how can i do

Doing backup: 20%

Because this doesnt work.

Any ideas, sorry for so many questions
 
Old 03-28-2009, 01:17 PM   #6
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Quote:
Hello, I got a script which does backup for my databases...
printf 'Doing stuff:\n' is seems to work, but how can i do
Perhaps if you just posted your entire script (In CODE tags please - Just click Advanced" at the bottom of this quick-reply window, then the # button) this would be easier to solve for you.

Maybe you just need to use some ANSI control codes ( http://www.termsys.demon.co.uk/vtansi.htm ) to reset the cursor to the beginning of the line before you print the % done bit.
 
Old 03-28-2009, 01:27 PM   #7
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Here it is:

PHP Code:

echo -en "Running mysql backup: "
for idb in $DB; do
        
DBDONE=$(expr $DBDONE 1)
        
do_dump $idb ${BACKUPDIR}/${idb}.sql >/dev/null 2>&1DBERR=$?
        if [ 
$DBERR -eq 0 ]; then
                DBPASS
=$(expr $DBPASS 1)
        else
                
DBFAIL=$(expr $DBFAIL 1)
        
fi
        
if [ -${BACKUPDIR}/${idb}.sql ]; then
                do_archiv 
${BACKUPDIR}/${idb}.sql >/dev/null 2>&1FILEERR=$?
                if [ 
$FILEERR -eq 0 ]; then
                        GZPASS
=$(expr $GZPASS 1)
                else
                        
GZFAIL=$(expr $GZFAIL 1)
                
fi
        
else
                
ERRONFILE="$ERRONFILE ${idb}.sql"
        
fi
        PER
=$(echo "scale=2; ${DBDONE}/${DBCOUNT}*100"|bc -l|awk '{$1=sprintf("%.0f",$1)}1')
        
printf '\r%d%% done' $PER
done 
So any ideas?! Because "Running mysql backup:" ends up like this:

11% donemysql backup:
 
Old 03-28-2009, 01:44 PM   #8
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Maybe just change:
Code:
echo -en "Running mysql backup: "
to
Code:
echo "Running mysql backup: "
But if you really want it all on the one line, you are going to have to use some ANSI escape sequences from the link I gave you, to reset the cursor to the correct position on the current line before you do the printf
 
Old 03-28-2009, 02:38 PM   #9
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
Well echo doesnt work, like I want too:

Running mysql backup:
43% done

and I would aspect Running mysql backup: 43% done

and with the ANSI stuff, can u give me an example, never did this before so really dont know.
 
Old 03-28-2009, 05:45 PM   #10
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Quote:
Well echo doesnt work.. [SNIP] ..and with the ANSI stuff, can u give me an example, never did this before so really dont know.
You need to do a bit of reading, research and learning. You also need to learn how to write, spell, punctuate, and use capitals, but I digress.

I am not going to do this all for you, but I'll happily give you an example to help you get started:

Try this in a terminal:
Code:
cat /var/log/messages
Your terminal will be filled with text.

Now enter:
Code:
echo -ne "\033[5;10f"
Now answer these questions for yourself, and feel free to experiment:

- What has happened to your cursor?
- Where is it in your terminal screen?
- Why did it move there?
- What happens if you just press <RETURN>
- What happens when you enter a command?

Now re-read the link I gave you to the ANSI Escape codes.

Then you'll find the answer yourself, and maybe will have learnt something that you can usefully use in the future.
 
Old 03-28-2009, 06:28 PM   #11
robertjinx
Member
 
Registered: Oct 2007
Location: Prague, CZ
Distribution: RedHat / CentOS / Ubuntu / SUSE / Debian
Posts: 749

Original Poster
Rep: Reputation: 73
PHP Code:
You also need to learn how to writespellpunctuate, and use capitalsbut I digress
If you wanna help, help, if not than please do not comment, English is not my mother language and of course errors will happen. If you do not like it, then just ignore my post, its not really that hard.
I asked for help from anyone who may know and wanna help out, I didnt ask an opinion on my English skills, if I would, you'll be the first to be informed.

Thanks!
 
  


Reply



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
bash cut command to get df percentage nutthick Programming 8 12-16-2010 11:02 AM
Percentage of all Code Written in C jhwilliams Programming 1 09-09-2007 11:11 PM
Free memory in percentage kinetik Programming 6 03-31-2006 12:16 AM
Percentage of zero post users ? samael26 LQ Suggestions & Feedback 5 05-29-2005 04:18 PM
What is the percentage of each distro in use ? newlin Linux - General 1 08-03-2003 04:41 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Server

All times are GMT -5. The time now is 07:03 PM.

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