I am writing a script where I am looking for help. I am using date format in my script as below (YYYY MM DD HH MM):
$ echo $DT
202002252216
Now my requirement is to compare two dates listed in the file and calculate output in hours.
cat /home/postgres/pglinux_mm_cfg
MM_START=202002261015
MM_END=202002261530
MM_SERVER=pglinux
Here is my script, timediff.sh
ENV=/home/postgres
MM_START=`grep MM_START ${ENV}/\`hostname\`_mm_cfg |awk -F"=" '{print $2}'`
MM_END=`grep MM_END ${ENV}/\`hostname\`_mm_cfg |awk -F"=" '{print $2}'`
MPHR=60
STARTIME=$(date +%Y%m%d%H%M -d ${MM_START})
ENDTIME=$(date +%Y%m%d%H%M -d ${MM_END})
MINUTES=$(( ($ENDTIME - $STARTIME) / $MPHR ))
echo $STARTIME $ENDTIME $MINUTES
How to get date difference between MM_END AND MM_START in day's and hours?
I am using ksh
.
"$MM_START"
instead of just$MM_START
, for example). Also, start your script with#!/bin/ksh
or whatever fits the path for your shell. Finally, do not use all capitals for your variable names - they are likely to clash with existing reserved names - instead use lowercase. – Chris Davies Feb 25 '20 at 22:51