- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Script to get the time difference in UNIX Shel...
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2004 02:05 AM
07-09-2004 02:05 AM
For example
begin_time=12,20,00
end_time=16,40,00
time_diff = ${end_time} - ${begin_time}
The result should be 04,20,00
Thank You,
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2004 02:07 AM
07-09-2004 02:07 AM
Re: Script to get the time difference in UNIX Shell
Anil
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2004 03:05 AM
07-09-2004 03:05 AM
Re: Script to get the time difference in UNIX Shell
-----------------------------------
#!/bin/bash
h1=`echo $1 | cut -c1-2` # Get Start Hour
m1=`echo $1 | cut -c3-4` # Get Start Minute
s1=`echo $1 | cut -c5-6` # Get Start Second
h2=`echo $2 | cut -c1-2` # Get Stop Hour
m2=`echo $2 | cut -c3-4` # Get Stop Minute
s2=`echo $2 | cut -c5-6` # Get Stop Second
s3=`expr $s2 - $s1`
if [ $s3 -lt 0 ]
then
s3=`expr $s3 + 60`
m1=`expr $m1 + 1`
fi
m3=`expr $m2 - $m1`
if [ $m3 -lt 0 ]
then
m3=`expr $m3 + 60`
h1=`expr $h1 + 1`
fi
h3=`expr $h2 - $h1`
if [ $h3 -lt 0 ]
then
h3=`expr $h3 + 24`
fi
for number in $h3 $m3 $s3
do
if [ $number -lt 10 ]
then
echo -n "0$number "
else
echo -n "$number "
fi
done
echo ""
-----------------------------------
./test.sh 122000 164000
04 20 00
Regards,
Sergejs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-09-2004 03:11 AM
07-09-2004 03:11 AM
SolutionThis should do it but it does assume that the maximum wrap is 1 day.
--------------------------------------------
#!/usr/bin/sh
to_seconds()
{
typeset T=${1}
shift
typeset -i10 HR=0
typeset -i10 MIN=0
typeset -i10 SEC=0
typeset -i10 SECONDS=0
echo "${T}" | awk -F ',' '{print $1,$2,$3}' | read HR MIN SEC
typeset -i10 STAT=${?}
SECONDS=$(( (${HR} * 3600) + (${MIN} * 60) + ${SEC} ))
echo ${SECONDS}
return ${STAT}
} # to_seconds
to_hhmmss()
{
typeset -i10 SECONDS=${1}
shift
typeset -i10 HR=0
typeset -i10 MIN=0
typeset -i10 SEC=0
HR=$((${SECONDS} / 3600))
MIN=$(( (${SECONDS} % 3600) / 60 ))
SEC=$(( ${SECONDS} % 60 ))
printf "%02d,%02d,%02d\n" ${HR} ${MIN} ${SEC}
return 0
} # to_hhmmss
begin_time="12,20,00"
end_time="16,40,00"
begin_seconds=$(to_seconds ${begin_time})
end_seconds=$(to_seconds ${end_time})
typeset -i10 DIFF=$((${end_seconds} - ${begin_seconds}))
if [[ ${DIFF} -lt 0 ]]
then
((DIFF += 86400))
fi
echo "Diff: ${DIFF}"
TIMEDIFF=$(to_hhmmss ${DIFF})
echo "Diff: ${TIMEDIFF}"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2004 05:48 PM
07-11-2004 05:48 PM
Re: Script to get the time difference in UNIX Shell
We can do it with a simple script as,
# cat forum.ksh
set -x
# Calculate the time which the child process consumed for the Network authentication to SUT
time_cal()
{
time1=$1
time2=$2
diff=0
hour1=$(($(echo $time1 | cut -d , -f 1)*3600))
min1=$(($(echo $time1 | cut -d , -f 2)*60))
sec1=$(echo $time1 | cut -d , -f 3)
hour2=$(($(echo $time2 | cut -d , -f 1)*3600))
min2=$(($(echo $time2 | cut -d , -f 2)*60))
sec2=$(echo $time2 | cut -d , -f 3)
# Assign the difference in seconds to a variable
diff=$(($hour2+$min2+$sec2-$hour1-$min1-$sec1))
}
begin_time=$1
end_time=$2
time_cal $2 $1
result=$diff
hr=$(($result/3600))
div1=$(($result%3600))
min=$(($div1/60))
sec=$(($div1%60))
hours=$hr
[[ $hr -lt 10 ]] && hours="0$hr"
mins=$min
[[ $min -lt 10 ]] && mins="0$min"
secs=$sec
[[ $sec -lt 10 ]] && secs="0$sec"
echo $hours,$mins,$secs
Regards,
Muthukumar.