Mohd
Mohd

Reputation: 148

Bash script to calculate time difference

I would appreciate any kind of help.I am a beginner in the bash script.

I am trying to subtract several times from the time of the start to get a list of time in seconds. See my input file how I did. It didn't work, if anyone can help on that, I would appreciate it.

#!/bin/bash

# start time

TIME=10:46:20

# recorded time

TIME_Record=(
11:03:00
11:24:00
11:27:00
11:32:00
)


SEC1=`date +%s -d ${TIME}`
SEC2=`date +%s -d ${TIME_Record}`


DIFFSEC=`expr ${SEC2} - ${SEC1}` 

Upvotes: 1

Views: 4656

Answers (1)

Alfe
Alfe

Reputation: 59416

I guess you are just having difficulties to formulate the loop in bash syntax. Here you go:

#!/bin/bash
START_TIME=10:46:20
TIME_Record=(
11:03:00
11:24:00
11:27:00
11:32:00
)
SEC1=$(date +%s -d "${START_TIME}")
for d in "${TIME_Record[@]}"
do
  SEC2=$(date +%s -d "$d")
  DIFFSEC=$(( SEC2 - SEC1 ))
  echo "$DIFFSEC"
done

Upvotes: 1

Related Questions