How to compare two String Time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare two String Time
# 1  
Old 06-01-2010
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.

Code:
#!/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.
Code:
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
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
Code:
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

Code:
./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

# 2  
Old 06-01-2010
Quote:
Originally Posted by akabir77
Code:
./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 ----------

Quote:
Originally Posted by akabir77
Code:
    echo FTP_SEC=$FTP_H_SEC

That can't work, try either
Code:
FTP_SEC=$FTP_H_SEC
echo $FTP_SEC

or why not simply
Code:
echo $FTP_H_SEC

The result is the same.
# 3  
Old 06-01-2010
Quote:
Originally Posted by pseudocoder
That's most likely because you had whitespaces (before and after the = sign) when you were assigning values to the above variables.
I took out the white space and that error is gone but I get the whole formula instead of a number

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

I changed my code as bellow now
Code:
	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
Code:
10*24*60+14*60+28

why is it not doing the calculation?

Same problem when I am doing this
Code:
       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!!!
# 4  
Old 06-01-2010
Check this and try to adapt it for your needs, hope the logic is clear and right.
Code:
#!/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

This User Gave Thanks to pseudocoder For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenate a string and number and compare that with another string in awk script

I have below code inside my awk script if ( $0 ~ /SVC IN:/ ) { svc_in=substr( $0,23 , 3); if (msg_start == 1 && msg_end == 0) { msg_arr=$0; } } else if ( $0 ~ /^SVC OUT:/ ) { svc_out=substr( $0, 9, 3); if (msg_start == 1 && msg_end == 0) ... (6 Replies)
Discussion started by: bhagya123
6 Replies

2. Shell Programming and Scripting

Compare two time (HH:MM:SS) - ksh

Is it possible to compate two time (24-hour format)? What I need is to just check if the first time is earlier than the second time. ex: ./script.ksh 09:30:15 13:00:34 = okay ./script.ksh 05:25:00 02:30:50 = invalid arguments (1 Reply)
Discussion started by: erin00
1 Replies

3. UNIX for Dummies Questions & Answers

Converting string date time to unix time in AWK

I'd like to convert a date string in the form of sun aug 19 09:03:10 EDT 2012, to unixtime timestamp using awk. I tried This is how each line of the file looks like, different date and time in this format Sun Aug 19 08:33:45 EDT 2012, user1(108.6.217.236) all: test on the 17th ... (2 Replies)
Discussion started by: bkkid
2 Replies

4. Shell Programming and Scripting

Compare dates with time

Hi Team, I need to compare three dates and extract the greatest among them into a file. 21 Jan 2012 05:46:59,146 21 Jan 2012 02:12:30,113 17 Jan 2012 09:08:10,417 Please help regarding the same. Thanks in advance..!!! Please use tags where appropriate, thank you (6 Replies)
Discussion started by: jaituteja
6 Replies

5. Shell Programming and Scripting

time from 2 files to compare

In first file say first.txt, i have a content say 14:56. In second file, say second.txt i have content say 16:01.... I want to compare if these two times in these 2 files are having a difference of 15 minutes...Can any one please help? (2 Replies)
Discussion started by: manoj.b
2 Replies

6. Shell Programming and Scripting

how to compare time in ksh

Hi I have several variables that hold time in the following format: A=01:30 B=23:35 C=22:45 Is there a way / Unix utility which can allow the to compare those time values? Thanks a lot -A (4 Replies)
Discussion started by: aoussenko
4 Replies

7. Shell Programming and Scripting

Compare Last Modified Time across Time Zone

Hi, I'm new to shell script programming, I only have Java programming background. I'm writing a shell script to do file synchronization between 2 machines that located at different time zone area. Both machine were set its time zone according to its geographical location (Eg: server is at... (1 Reply)
Discussion started by: python
1 Replies

8. Shell Programming and Scripting

Can we compare time ...

Hi ... Is there a possibility that timings from two files be compared?? If i have a set of files in a folder and i want to get the files only upto a particular time. eg: If i have the files staff 2388 Oct 3 04:33 x.ksh staff 3628 Oct 3 06:57 1.ksh staff 32383... (1 Reply)
Discussion started by: sparks
1 Replies

9. UNIX for Dummies Questions & Answers

Need to get 4 Hrs back time and compare with successive time

Hi all, I am working on a script in which i need to get 4 hrs back time from the current time which i got from this perl function : `perl -e 'print localtime(time() - 14400) . "\n"'` now i need to get this in a loop and increment that time by 15 minutes i.e i=900(=15minutes) `perl... (2 Replies)
Discussion started by: maanik85
2 Replies

10. Shell Programming and Scripting

Compare file time

i need to write script where I need to keep monitoring a files timestamp, if it changes, I need to run another abc.sh script. I am thinking I can save file's current timestamp in another file or enviornment variable and after 10 min compare the files timestamp with the original timestamp. If... (1 Reply)
Discussion started by: pdr302
1 Replies
Login or Register to Ask a Question