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
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
clx
2
You might want to look here.
Also here.
1 Like
If you want to calculate percentage by using one expression, use -
echo "Percentage% =" `expr \( \( 8500 - 8000 \) \* 100 \) / 2000`"%"
1 Like
This little example should show you output that stays on one line:
$ 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
$
1 Like
Thanks gary_w
By changing echo it worked in bash shell.
here is the code for bash
#!/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
Actually, this works for me on my version of bash:
echo -en "\r$i"
The "n" argument means the "\c" is not needed, and neither is the "\b".
Alternatively, this works too:
echo -e "\r$i\c"