title: Calculate the Time difference in bash Script
body: <p>I need to calculate the time difference between RMAN start and RMAN End. Log is like this</p>
  
  <pre><code>-rw-r----- oracle/dba 2017-03-11 21:56:35 storage/backup/daily/ALG_PDB_2drut5jf.alg
  -rw-r----- oracle/dba 2017-03-11 21:56:34 storage/backup/daily/ALG_PDB_2erut5jf.alg
  -rw-r----- oracle/dba 2017-03-11 21:56:07 storage/backup/daily/ALG_PDB_2frut5jf.alg
  -rw-r----- oracle/dba 2017-03-11 21:56:29 storage/backup/daily/ALG_PDB_2grut5jv.alg
  -rw-r----- oracle/dba 2017-03-11 21:39:14 storage/backup/daily/BKPPDB_28rut42s.F_bkp
  -rw-r----- oracle/dba 2017-03-11 21:47:28 storage/backup/daily/BKPPDB_29rut42s.F_bkp
  -rw-r----- oracle/dba 2017-03-11 21:42:41 storage/backup/daily/BKPPDB_2arut42s.F_bkp
  -rw-r----- oracle/dba 2017-03-11 21:55:33 storage/backup/daily/BKPPDB_2brut4k8.F_bkp
  -rw-r----- oracle/dba 2017-03-11 21:55:36 storage/backup/daily/c-2819904582-20170311-00
  -rw-r----- oracle/dba 2017-03-11 21:56:42 storage/backup/daily/c-2819904582-20170311-01
  -rw-r----- oracle/dba 2017-03-11 21:56:41 storage/backup/daily/control_PDB_2hrut5kp.ctl
  </code></pre>
  
  <p>I wrote  a script that first sort the column based on 4th column which is Time , Below is the script </p>
  
  <pre><code>#!/bin/bash -x
  if [ -f /storage/backup/weekly/DB-Backup-$(date -d "yesterday" '+%a-%d%m%y').tgz ]
   then
  
  export StartMin=$(tail -30 /storage/backup/weekly/DailyCompressionLog|grep "^-\|^ -" |sort -k5|awk '{print $5}' |awk -F ":" '{print $2}'|awk 'NR==1; END{print}'|awk 'FNR==1 {print $1}')
  export EndMin=$(tail -30 /storage/backup/weekly/DailyCompressionLog|grep "^-\|^ -" |sort -k5|awk '{print $5}' |awk -F ":" '{print $2}'|awk 'NR==1; END{print}'|awk 'FNR==2 {print $1}')
  
  export StartHour=$(tail -30 /storage/backup/weekly/DailyCompressionLog|grep "^-\|^ -" |sort -k5|awk '{print $5}' |awk -F ":" '{print $1}'|awk 'NR==1; END{print}'|awk 'FNR==1 {print $1}')
  export EndHour=$(tail -30 /storage/backup/weekly/DailyCompressionLog|grep "^-\|^ -" |sort -k5|awk '{print $5}' |awk -F ":" '{print $1}'|awk 'NR==1; END{print}'|awk 'FNR==2 {print $1}')
  
  echo "Backup for `date -d "yesterday" '+%a-%d%m%y'`" &gt;&gt; /storage/backup/weekly/MonthlyCompressionLog
  echo -e "RMAN Backup Started at $StartHour":"$StartMin" &gt;&gt; /storage/backup/weekly/MonthlyCompressionLog
  echo -e "RMAN Backup Ended   at $EndHour":"$EndMin" &gt;&gt; /storage/backup/weekly/MonthlyCompressionLog
  echo -e "Time taken to Complete Backup Job Hours:Mins $((EndHour-StartHour))":"$((EndMin-StartMin))" &gt;&gt; /storage/backup/weekly/MonthlyCompressionLog
  
  echo -e "-------------------------------------------------------\n" &gt;&gt; /storage/backup/weekly/MonthlyCompressionLog
  </code></pre>
  
  <h1>When Script works (Success)</h1>
  
  <p>If End Time is Greater Than StartTime</p>
  
  <h1>When Script Fails</h1>
  
  <p>In current state if the Start Time is great than End Time , In this case Endhour will be greater than the StartHour</p>
  
  <p>Scenario :: Lets say if RMAN started at 14:56 and completes at 15:02 , In this case total Time from Start to End is just 00:06 Min but script will show 1:-54 </p>
  
  <p>Any ideas ....</p>