Time Diff in shell script | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Time Diff in shell script

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 08-25-2010
posner posner is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 5 August 2013, 1:54 PM EDT
Posts: 37
Thanks: 19
Thanked 3 Times in 3 Posts
Time Diff in shell script

Hi all ,

i am trying to calculate time difference btw the script execution

I am using solaris


Code:
start_time=`date +%s` 
sleep 2
end_time=`date +%s`
duration=`expr $end_time - $start_time`
when   i try to subtract i get the error 
 line 13: %s - -time : syntax error: operand expected (error token is "%s - -time ")

i think start_time=`date +%s` doesn't work in solaris because this is working in other linux box

later on i calculate the time to minutes

Code:
minutes=$(echo "scale=2; $duration / 60" | bc)

which gives some error in seconds but thats ok
and i cant calculate again to hours which gives errors in minutes

so all i am trying to achieve is

Code:
#!bin/bash

start_time=??

--script logic

end_time=??
 
durtation=??

echo "$duration"

this on a solaris system

thanks in advance

Last edited by Scott; 08-25-2010 at 07:40 PM.. Reason: Use code tags next time. Thanks in advance :)
Sponsored Links
    #2  
Old 08-25-2010
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 21 March 2014, 6:30 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 117 Times in 110 Posts
Quote:
Originally Posted by posner View Post

Code:
 line 13: %s - -time : syntax error: operand expected (error token is "%s - -time ")

Did you check line 13 ?

Anyway :

Code:
#!/bin/sh
start_time=`date +%s`
sleep 2
end_time=`date +%s`
duration=`expr $end_time - $start_time`
echo $duration

or..

Code:
#!/bin/bash
start_time=`date +%s`
sleep 2
end_time=`date +%s`
echo $(($end_time-$start_time))

Sponsored Links
    #3  
Old 08-25-2010
posner posner is offline
Registered User
 
Join Date: Jun 2010
Last Activity: 5 August 2013, 1:54 PM EDT
Posts: 37
Thanks: 19
Thanked 3 Times in 3 Posts
when i do this
start_time=`date +%s`
and then say
echo "$start_time"
the result is "%s" this is on Solaris box where as in Linux it gives time in seconds

@danmero
so doing
echo $(($end_time-$start_time))
or
duration=`expr $end_time - $start_time`
doesn't matter as i dont have the right $start_time and $end_time

and that line 13 error because i trying to subtract %s with %s
duration=`expr $end_time - $start_time`
    #4  
Old 08-25-2010
agama agama is offline Forum Advisor  
Always Learning
 
Join Date: Jul 2010
Last Activity: 7 April 2014, 3:02 PM EDT
Location: earth>US>UTC-5
Posts: 1,466
Thanks: 110
Thanked 502 Times in 481 Posts
Unless you have a non-Posix flavour of date you'll have the dickens of a time trying to work magic with date arithmetic. If all you want is the wall clock time (real time) that a process (script, or programme) took to run then try this:


Code:
#!/usr/bin/env ksh 
#ksh is cleaner because ksh gets it right when reading from a pipe into variables
(time -p sleep 5) 2>&1 | read junk real; 
echo $real


Code:
#!/usr/bin/env bash
# if you have to, messier because bash buggers reading from pipes
real=$( (time -p sleep 5) 2>&1 )
real=${real#real }
echo ${real%%user*}

What I have done in the past is to write a simple C programme that prints the current time as an integer and use that to capture the time before and after a series of commands. Not all that accurate, but if your commands are running for a few minutes then the time to load and execute the little programme twice can be neglected. It was also quicker to do that than to dork round with date and time when AT&T AST or GNU tools aren't available.


Code:
start=$( itime )         # integer time in seconds
# command(s)
end=$( itime )

echo "duration: $(( $end - $start ))s"

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Shell script using Diff DallasT Shell Programming and Scripting 10 05-03-2010 03:30 PM
Process diff command output in a shell script bhaliyajalpesh Shell Programming and Scripting 4 12-11-2009 04:25 AM
time diff help uwork72 Shell Programming and Scripting 2 10-25-2008 09:45 AM
Shell script to manipulate a message count for the same IP @ diff session gobinath Shell Programming and Scripting 1 05-28-2008 03:13 PM
diff in date/time ? ali560045 Shell Programming and Scripting 1 02-20-2008 12:30 PM



All times are GMT -4. The time now is 02:58 PM.