Daniel Daniel - 3 months ago 12
Bash Question

Covert array date string and decrease 3 hours in Shell Script

I need from an array decrease 3 hours from it and print in command line again

the output of the array is:

12:29:14
14:10:26
14:30:34
14:35:31
14:35:10
11:57:03
12:49:31
12:57:09
11:04:49
13:06:57
14:35:34
14:30:49
14:34:28
12:58:02
14:28:12
12:29:18
11:33:23
12:27:04
14:35:43
12:58:46


I am trying with this line, but no use.

date2=$(date -d "${array[@]}" "3 hour ago" +"%H:%M:%S")


Could you please help me about it?

Answer

try this;

#!/bin/bash
times=( "12:29:14" "14:10:26" "14:30:34" "14:35:31" "14:35:10" "11:57:03" "12:49:31" "12:57:09" "11:04:49" "13:06:57" "14:35:34" "14:30:49" "14:34:28" "12:58:02" "14:28:12" "12:29:18" "11:33:23" "12:27:04" "14:35:43" "12:58:46")

for dateItem in ${times[@]}
do
date2=$(date --date="$dateItem 3 hour ago" +%H:%M:%S)
echo $date2
done
Comments