How to compare two String Time

I am trying to compare two string time to figure out the difference.

I need to find out the start date of a process is running for over certain minutes.

so far I was able to get the process start time and system time. Now I need to figure out the difference between two time in minutes.

Here is my code so far: batch_output_x is the process i am trying to see if its running over certain minutes or not.

#!/usr/bin/ksh

. ~/gateway/bin/gateway_env.sh

VAR=$(ps -ef | grep batch_output_x )

        FTP_HOUR=$(echo $VAR|cut -c23-24)
        FTP_MIN=$(echo $VAR|cut -c26-27)
        FTP_SEC=$(echo $VAR|cut -c29-30)

    FTP_HOUR_MIN_SEC=$FTP_HOUR:$FTP_MIN:$FTP_SEC
    RUN_TIME=`date +'%H:%M:%S'`

Now how do i get the time difference in minute?

if i do echo on FTP_HOUR_MIN_SEC and RUN_TIME this is what i get.

RUN_TIME=09:28:23
FTP_HOUR_MIN_SEC=09:28:23

---------- Post updated at 10:00 AM ---------- Previous update was at 09:36 AM ----------

I am trying to convert the times into second and then compare with this code

FTP_H_SEC = (($FTP_HOUR*24)*60+$FTP_MIN)*60+$FTP_SEC

But I get an error. to figure out the bug I separated the calculation

FTP_HOUR_SEC = $FTP_HOUR*24
    FTP_MIN_SEC    = $FTP_HOUR_SEC*60+$FTP_MIN
    FTP_H_SEC = $FTP_MIN_SEC*60+$FTP_SEC

    echo FTP_SEC=$FTP_H_SEC

but this is the out put i am getting

./chk_process.sh[27]: FTP_HOUR_SEC:  not found
./chk_process.sh[28]: FTP_MIN_SEC:  not found
./chk_process.sh[29]: FTP_H_SEC:  not found

That's most likely because you had whitespaces (before and after the = sign) when you were assigning values to the above variables.

---------- Post updated at 17:12 ---------- Previous update was at 17:10 ----------

That can't work, try either

FTP_SEC=$FTP_H_SEC
echo $FTP_SEC

or why not simply

echo $FTP_H_SEC 

The result is the same.

I took out the white space and that error is gone but I get the whole formula instead of a number

FTP_SEC=10*24*60+12*60+00

I changed my code as bellow now

	FTP_HOUR_SEC=$FTP_HOUR*24
	FTP_MIN_SEC=$FTP_HOUR_SEC*60+$FTP_MIN
	FTP_H_SEC=$FTP_MIN_SEC*60+$FTP_SEC

	echo $FTP_H_SEC 

and out put is

10*24*60+14*60+28

why is it not doing the calculation?

Same problem when I am doing this

       RUN_TIME=`date +'24*%H+60*%M+%S'`


	echo RUN_TIME=$RUN_TIME

output:
RUN_TIME=24*10+60*16+17

instead of doing the math its printing the formula!!!

Check this and try to adapt it for your needs, hope the logic is clear and right.

#!/bin/sh

RUN_TIME=$(date +'%H:%M:%S')

echo $RUN_TIME

H=$(echo $RUN_TIME | cut -c1-2)
M=$(echo $RUN_TIME | cut -c4-5)
S=$(echo $RUN_TIME | cut -c7-8)

echo $H
echo $M
echo $S

SEC_FROM_H=$(($H*60*60))
echo $SEC_FROM_H

SEC_FROM_M=$(($M*60))
echo $SEC_FROM_M

TOTAL_SEC=$(($SEC_FROM_H+$SEC_FROM_M+$S))
echo $TOTAL_SEC
1 Like