# Shell Script Statistics

**URL:** <https://community.unix.com/t/shell-script-statistics/300826>\
**Category:** Shell Programming and Scripting\
**Created:** [December 9, 2011, 6:16am UTC](https://community.unix.com/t/shell-script-statistics/300826 "2011-12-09T06:16:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ambarginni](https://community.unix.com/letter_avatar/ambarginni/32/6_22734026db12803ebb3e059622ac3534.png) [@ambarginni](https://community.unix.com/u/ambarginni)\
**Post date:** [December 9, 2011, 6:16am UTC](https://community.unix.com/t/shell-script-statistics/300826/1 "2011-12-09T06:16:17Z")

</div>

Hi All,

I am writing a UNIX shell script for the Database Operations and wanted to take the statistics i.e. How much time my script has taken to complete the operation.

I wanted to apply the logic say: In the beginning of the script I will store the value of current date and time in some variable and then finally I will take the difference of that datetime variable from the current date and time.

I am very new to shell scripting. Can some one help me to put this logic into the shell script code.

Let me know if any new logic will hold good for my requirments.

---

<div class="post-metadata">

**Author:** ![itkamaraj](https://community.unix.com/user_avatar/community.unix.com/itkamaraj/32/1880_2.png) [@itkamaraj](https://community.unix.com/u/itkamaraj)\
**Post date:** [December 9, 2011, 6:21am UTC](https://community.unix.com/t/shell-script-statistics/300826/2 "2011-12-09T06:21:11Z")

</div>

use time command

```nohighlight
 
$time yourscript.sh

```

---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [December 9, 2011, 6:25am UTC](https://community.unix.com/t/shell-script-statistics/300826/3 "2011-12-09T06:25:56Z")

</div>

```nohighlight
$ time ./script.sh
or
$ time sh script.sh

```

---

<div class="post-metadata">

**Author:** ![ambarginni](https://community.unix.com/letter_avatar/ambarginni/32/6_22734026db12803ebb3e059622ac3534.png) [@ambarginni](https://community.unix.com/u/ambarginni)\
**Post date:** [December 9, 2011, 6:49am UTC](https://community.unix.com/t/shell-script-statistics/300826/4 "2011-12-09T06:49:49Z")

</div>

I want to write this statistics into a log file (writing logs through the same script).... When I used the above suggestions in my script it says can't execute the script.sh ☹

---

<div class="post-metadata">

**Author:** ![balajesuri](https://community.unix.com/user_avatar/community.unix.com/balajesuri/32/1423_2.png) [@balajesuri](https://community.unix.com/u/balajesuri)\
**Post date:** [December 9, 2011, 6:55am UTC](https://community.unix.com/t/shell-script-statistics/300826/5 "2011-12-09T06:55:26Z")

</div>

```nohighlight
#! /bin/bash

start=`date +%s%N`

echo "hello"
sleep 1
echo "world"
sleep 1

end=`date +%s%N`
dif=$(($end - $start))

echo "Execution time in nanoseconds: $dif"

```

You can do an appropriate conversion to seconds using the bench calculator.

---

<div class="post-metadata">

**Author:** ![ambarginni](https://community.unix.com/letter_avatar/ambarginni/32/6_22734026db12803ebb3e059622ac3534.png) [@ambarginni](https://community.unix.com/u/ambarginni)\
**Post date:** [December 12, 2011, 12:42am UTC](https://community.unix.com/t/shell-script-statistics/300826/6 "2011-12-12T00:42:34Z")

</div>

It Worked for me....

Thanks Again  
Ambar Agrawal
