Pages

Wednesday, December 16, 2015

Sample script to convert EPOC time-stamp to Normal time-stamp


Requirement:-

To convert time-stamp in EPOC format to normal format

Inputs:-

Input should be time-stamp in EPOC format

Outputs:-

Script will convert the EPOC time-stamp input to normal format time-stamp

Script:-

#!/bin/bash
# To check whether input argument is passed to the script or not
if [[ -z "$1" ]]
then
    # To check whether input to script is passed using pipe operator
    if [[ -p /dev/stdin ]]
    then
        dt=`cat -`
        # To convert input argument data of Unix Epoc to Normal Timestamp pattern
        date -d @$dt +%c                     # Format with spaces
        # To convert input argument data of Unix Epoc to Normal Timestamp pattern without any space (For filename usage)
        date -d @$dt +%a-%b-%e-%H.%M.%S-%Z-%Y        # Format with spaces removed
    else
        echo "No Timestamp given." >&2
        exit
    fi
else
    dt=$1
    # To convert input argument data of Unix Epoc to Normal Timestamp pattern
    date -d @$dt +%c                    # Format with spaces
    # To convert input argument data of Unix Epoc to Normal Timestamp pattern without any space (For filename usage)
    date -d @$dt +%a-%b-%e-%H.%M.%S-%Z-%Y       # Format with spaces removed

fi


No comments:

Post a Comment