how to print the percentage of task completed on the same line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to print the percentage of task completed on the same line
# 1  
Old 05-29-2012
how to print the percentage of task completed on the same line

Hi

I have written a utility in shell program for which i want to add a code to display percentage of completion dynamically

My scripts runs approx about 30 to 45min , It appends exactly 2000 lines to one of the log file.

How to calculate percentage ?
I will note the total number of lines in the log file before running scripts.
Lets say Initial=8000.

Final is the total number of lines after execution
Final=8000+2000

In a loop for every 5 seconds i will count the lines in log file and store in
Current=8500
Code:
Percentage % = 8500-8000/2000*100 = 25%

I want to display Percentage as increasing value in the same line rather than printing a new line for every 5 seconds.

Thanks
# 2  
Old 05-29-2012
You might want to look here.
Also here.
This User Gave Thanks to clx For This Post:
# 3  
Old 05-29-2012
If you want to calculate percentage by using one expression, use -
Code:
echo "Percentage% =" `expr \( \( 8500 - 8000 \) \* 100 \) / 2000`"%"

This User Gave Thanks to donadarsh For This Post:
# 4  
Old 05-29-2012
This little example should show you output that stays on one line:
Code:
$ cat x
#!/bin/ksh

integer i=0

while [[ $i -lt 10 ]]; do
##
##  \r = carriage return
##  \c = suppress linefeed
##
  echo "\r$i\c"
  (( i=i+1 ))
  sleep 1
done

echo

exit 0
$

This User Gave Thanks to gary_w For This Post:
# 5  
Old 05-30-2012
Thanks gary_w

By changing echo it worked in bash shell.

here is the code for bash

Code:
#!/bin/bash

i=0

while [[ $i -lt 10 ]]; do
##
##  \r = carriage return
##  \c = suppress linefeed
##
  echo -en "\r$i\c\b"
  (( i=i+1 ))
  sleep 1
done

echo

exit 0

# 6  
Old 05-30-2012
Actually, this works for me on my version of bash:
Code:
echo -en "\r$i"

The "n" argument means the "\c" is not needed, and neither is the "\b".
Alternatively, this works too:
Code:
echo -e "\r$i\c"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reading a file line by line and print required lines based on pattern

Hi All, i want to write a shell script read below file line by line and want to exclude the lines which contains empty value for MOUNTPOINT field. i am using centos 7 Operating system. want to read below file. # cat /tmp/d5 NAME="/dev/sda" TYPE="disk" SIZE="60G" OWNER="root"... (4 Replies)
Discussion started by: balu1234
4 Replies

2. Shell Programming and Scripting

Find line then evaluate text on next line, print when condition is met

Hello, I am looking for a specific situation in a text file. The conditions are, > <CompoundName> InChI=1S/C5H12NO2/c1-5(2)4-8-6(3)7/h5H,4H2,1-3H3/q+1 I am looking for cases where the line "> <CompoundName>" is followed by a line that contains the string "InChI=" without regard to... (5 Replies)
Discussion started by: LMHmedchem
5 Replies

3. UNIX for Dummies Questions & Answers

Print script is completed successfully or not

Hai guys I am running three shellscripts through Gtk2-Perl(GUI) these are the scripts Drccalibrescript1 script2 script3 Gtk2-Perl(GUI) drccalibre -> If I run this script through Gtk2-Perl(GUI) these are results of the drccalibrescript1 . summary/.results I have to find size of... (1 Reply)
Discussion started by: kiran425
1 Replies

4. Shell Programming and Scripting

How to do one line bash schedule task?

This seems to work: https://www.unix.com/shell-programming-scripting/179705-how-run-cygwin-bash-windows-scheduled-task.html However, I was hoping to avoid writing a 2 line bat files to invoke my cygwin scripts as a scheduled task (since I'm making lots scheduled tasks). I was hoping this would... (1 Reply)
Discussion started by: siegfried
1 Replies

5. Shell Programming and Scripting

Completed "Command line google translation tool"

This tool for access translate.google.com from terminal and English dictionary. main mirror https://github.com/Anoncheg1/Command-line-translator mirror Google translate from command line and some more features - Pastebin.com requirements: bash, cURL, SpiderMonkey, forvo.com account for... (0 Replies)
Discussion started by: 654321
0 Replies

6. Shell Programming and Scripting

Print pipe separated list as line by line in Korn Shell

Korn Shell in AIX 6.1 I want to print the below shown pipe (|) separated list line by line. line=es349889|nhb882309|ts00293|snh03524|bg578835|bg37900|rnh00297|py882201|sg175883 for i in line do echo "Hello $line " done I wanted to execute the above for loop. But i can't even set the... (3 Replies)
Discussion started by: polavan
3 Replies

7. Shell Programming and Scripting

Parse an XML task list to create each task.xml file

I have an task definition listing xml file that contains a list of tasks such as <TASKLIST <TASK definition="Completion date" id="Taskname1" Some other <CODE name="Code12" <Parameter pname="Dog" input="5.6" units="feet" etc /Parameter> <Parameter... (3 Replies)
Discussion started by: MissI
3 Replies

8. Shell Programming and Scripting

percentage by line

hi, I am new to awk.. and getting used to the scripts. I have a small data set 5 coulms.. 16 rows. 1) I am trying to remove the percentages of each line of colum 3..like first line divided the sum of colum 3 divided by 100 and print it out. removing hte percentages of each line I would really... (3 Replies)
Discussion started by: rockiefx
3 Replies

9. Shell Programming and Scripting

comment and Uncomment single task out of multiple task

I have a file contains TASK gsnmpproxy { CommandLine = $SMCHOME/bin/gsnmpProxy.exe } TASK gsnmpdbgui { CommandLine = $SMCHOME/bin/gsnmpdbgui.exe I would like to comment and than uncomment specific task eg TASK gsnmpproxy Pls suggest how to do in shell script (9 Replies)
Discussion started by: madhusmita
9 Replies
Login or Register to Ask a Question