简体   繁体   English

计算bash脚本中的时差

[英]Calculate the Time difference in bash Script

I need to calculate the time difference between RMAN start and RMAN End. 我需要计算RMAN开始和RMAN结束之间的时间差。 Log is like this 日志是这样的

-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

I wrote a script that first sort the column based on 4th column which is Time , Below is the script 我写了一个脚本,该脚本首先根据第4列Time对该列进行排序,下面是该脚本

#!/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'`" >> /storage/backup/weekly/MonthlyCompressionLog
echo -e "RMAN Backup Started at $StartHour":"$StartMin" >> /storage/backup/weekly/MonthlyCompressionLog
echo -e "RMAN Backup Ended   at $EndHour":"$EndMin" >> /storage/backup/weekly/MonthlyCompressionLog
echo -e "Time taken to Complete Backup Job Hours:Mins $((EndHour-StartHour))":"$((EndMin-StartMin))" >> /storage/backup/weekly/MonthlyCompressionLog

echo -e "-------------------------------------------------------\n" >> /storage/backup/weekly/MonthlyCompressionLog

When Script works (Success) 脚本工作时(成功)

If End Time is Greater Than StartTime 如果结束时间大于开始时间

When Script Fails 脚本失败时

In current state if the Start Time is great than End Time , In this case Endhour will be greater than the StartHour 在当前状态下,如果“开始时间”大于“结束时间”,在这种情况下,“结束时间”将大于“开始时间”

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 方案::假设RMAN是从14:56开始并在15:02结束,在这种情况下,从开始到结束的总时间仅为00:06 Min,但是脚本将显示1:-54

Any ideas .... 有任何想法吗 ....

Instead of calculating Hours and Minutes separately, calculate the number of minutes since midnight for Start and End, and perform arithmentic using that. 无需单独计算小时和分钟,而是计算自午夜起算的“开始”和“结束”分钟数,然后使用该值进行算术运算。

So something like : 所以像:

StartNumMins=$(( $StartHour * 60 + $StartMin ))
EndNumMinutes=$(( $EndHour * 60 + $EndMin ))

MinutesDiff=$(( $EndNumMinutes - $StartNumMins ))

DiffHours="0$(($MinutesDiff / 60))"
DiffMins="0$(($MinutesDiff % 60))"

echo "Diff is $MinutesDiff minutes, which is ${DiffHours:-2}:${DiffMins:-2}"

which produces : 产生:

Diff is 6 minutes, which is 00:06

Of course, if your process crosses midnight, then you'll have to take the start/end dates into consideration, not just the times. 当然,如果您的流程经过了午夜,那么您就必须考虑开始/结束日期,而不仅仅是时间。 There are a few options to do that (such as using GNU's 'date' if that's available to you - eg. see Convert an ISO date to seconds since epoch in linux bash ), but that's another issue. 有几种方法可以做到这一点(例如,如果可以使用GNU的“日期”,例如,请参见将ISO日期转换为linux bash中的纪元以来的秒数 ),但这是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM