user1228191
user1228191

Reputation: 681

How to show percentage of script execution in unix

If we run a shell/perl/tcl script in Unix how can we display the percentage of execution in the mean time?

Example:

./scriptname
1% Completed the execution of script.pl
2% Completed the execution of script.pl
...
100% Completed the the execution of script.pl

and display the execution time taken for the script at the end.

Example output:

Execution time for script.pl is :50 Seconds

Upvotes: 1

Views: 1651

Answers (5)

JRFerguson
JRFerguson

Reputation: 7516

In Perl, look at the Smart::Comments module.

And for the elapsed time, simply take the delta of the time() at the beginning versus the end.

Upvotes: 4

kev
kev

Reputation: 161644

#!/bin/bash

before=`date +%s`

# do task
for i in {1..10}; do
    # do steps
    sleep 1
    # show progress
    echo -ne "${i}0% Completed\r"
done

after=`date +%s`

duration=$((after-before))
echo "Execution time is: $duration Seconds"

Upvotes: 1

Stefan
Stefan

Reputation: 2068

as the time a script executes may differ each time (slightly), you need to do this from your script only, as the shell doesn't offer this feature.

i'd recommend to set some "milestones" after each function call or whatever step in your script with an integer that is increased each time by 1 or 0.x

this one may be tough, but it's the only way as far as i'm concerned.

Upvotes: 0

raina77ow
raina77ow

Reputation: 106385

We've implemented similar progress display with Time::Progress module, perhaps you'd check it.

Upvotes: 1

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81684

There are only two ways you could do this:

  1. The interpreter could support it natively; this isn't something bash, tclsh, or perl do, so you're out of luck.
  2. The script could include statements to do it by printing the needed info periodically.

Only the second one is generally applicable. Sorry.

Upvotes: 0

Related Questions