Dimu4
Dimu4

Reputation: 233

Bash script - convert time according to template

My script is capturing time from docker container and next step i want to convert it to some format so i can substitute docker time with current system time and get the difference.

So for now i want at least to convert it so my script will understand that i'm working with the time.

#!/bin/bash

name=$1
matching=$(docker ps -a --filter="name=$name" -q | xargs)
current_time=$(date -u +"%Y-%m-%dT%T")
echo Current time: ${current_time}

for i in $matching
do
    time=sudo docker inspect $i | grep -w "StartedAt" | awk '{gsub(/"/, "", $2); print $2 }' | head -n 1 | cut -c -19
    echo ${time}    
    echo $(date -d +"%Y-%m-%dT%H:%M:%S" --date="$time")
done

The output i'm getting is

Thu Apr 20 00:00:00 PDT 2017
2017-04-19T00:57:15

But i expect to get:

Wed Apr 19 00:57:15 PDT 2017
2017-04-19T00:57:15

Upvotes: 1

Views: 95

Answers (1)

Kadir
Kadir

Reputation: 1762

docker inspect command output is not assigned to time variable. Try command substitution:

#!/bin/bash

name=$1
matching=$(docker ps -a --filter="name=$name" -q | xargs)
current_time=$(date -u +"%Y-%m-%dT%T")
echo Current time: ${current_time}

for i in $matching
do
    time=$(sudo docker inspect $i | grep -w "StartedAt" | awk '{gsub(/"/, "", $2); print $2 }' | head -n 1 | cut -c -19)
    echo ${time}    
    echo $(date -d +"%Y-%m-%dT%H:%M:%S" --date="$time")
done

Upvotes: 1

Related Questions