# Time Difference

**URL:** <https://community.unix.com/t/time-difference/307494>\
**Category:** Shell Programming and Scripting\
**Created:** [March 30, 2012, 3:23am UTC](https://community.unix.com/t/time-difference/307494 "2012-03-30T03:23:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sreekanthragi](https://community.unix.com/letter_avatar/sreekanthragi/32/5_5575768a8748004e209b776fc1b2916d.png) [@sreekanthragi](https://community.unix.com/u/sreekanthragi)\
**Post date:** [March 30, 2012, 3:23am UTC](https://community.unix.com/t/time-difference/307494/1 "2012-03-30T03:23:28Z")

</div>

Is this way of finding the time difference a correct way of doing it or is it error-prone.

```nohighlight
# ****Check if lastrun exist. If exists check if the difference is 1 hour or not and act accordingly***********************
if [-f $LOG_DIR/lastrun]; then
	lastrun_time=`cat $LOG_DIR/lastrun`
	curr_time=`date +%s`

	#Here we are checking if the last run was within the last hour, if true we simply exit without running.	
	if [$((curr_time-lastrun_time)) -le 3600]; then
		#echo "File is being run within an hour of last run.exiting."
		exit
	else
		#Removing last run, if the last run is beyond an hour we remove the file and rerun the grep.
		rm -f LOG_DIR/lastrun
	fi	
fi

```

TIA  
Sreekanth

---

<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:** [March 30, 2012, 3:27am UTC](https://community.unix.com/t/time-difference/307494/2 "2012-03-30T03:27:33Z")

</div>

```plaintext
$(($curr_time - $lastrun_time))

```

Please use [code tags.](http://www.unix.com/how-post-unix-linux-forums/167686-forum-video-tutorial-how-use-code-tags.html)

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [March 30, 2012, 4:06am UTC](https://community.unix.com/t/time-difference/307494/3 "2012-03-30T04:06:25Z")

</div>

What does `$LOG_DIR/lastrun` contain ?

--  
Actually, POSIX compliant shells should not require dollar signs when using integers in an arithmetic expansion:

> [@](#):
>
> If the shell variable x contains a value that forms a valid integer constant, then the arithmetic expansions "$((x))" and "$(($x))" shall return the same value.

[Shell Command Language: 2.6.4 Arithmetic Expansion](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04)

---

<div class="post-metadata">

**Author:** ![sreekanthragi](https://community.unix.com/letter_avatar/sreekanthragi/32/5_5575768a8748004e209b776fc1b2916d.png) [@sreekanthragi](https://community.unix.com/u/sreekanthragi)\
**Post date:** [March 30, 2012, 4:11am UTC](https://community.unix.com/t/time-difference/307494/4 "2012-03-30T04:11:47Z")

</div>

Scutinizer,

lastrun contains the time when the script was last run, the same as `date +%s`

Tks,  
Sreekanth

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [March 30, 2012, 4:16am UTC](https://community.unix.com/t/time-difference/307494/5 "2012-03-30T04:16:41Z")

</div>

If it contains epoch time then I would think this is a valid way of finding the difference.

I noticed a $-sign is missing

```nohighlight
rm -f LOG_DIR/lastrun

```

It is a good practice to use double quotes around variable reference:

```nohighlight
rm -f "$LOG_DIR/lastrun"

```

Although I am sure the path to your log dir will not contain spaces or funny characters..

You might like to use something like:

```nohighlight
exit 1

```

So you have a return code..
