Time Diff in shell script

Hi all ,

i am trying to calculate time difference btw the script execution

I am using solaris

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

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

#!bin/bash

start_time=??

--script logic

end_time=??
 
durtation=??

echo "$duration"

this on a solaris system

thanks in advance

Did you check line 13 ?

Anyway :

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

or..

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

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`

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:

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

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

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