Skip to content

Instantly share code, notes, and snippets.

@jstine35
Last active September 25, 2024 07:47
Show Gist options
  • Select an option

  • Save jstine35/e0fc0e06ec06d74bc3ebd67585bf2a1d to your computer and use it in GitHub Desktop.

Select an option

Save jstine35/e0fc0e06ec06d74bc3ebd67585bf2a1d to your computer and use it in GitHub Desktop.

Revisions

  1. jstine35 revised this gist Aug 24, 2024. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions timestamp_trick.sh
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,9 @@

    # -------------------------------------------------------------------------------------
    # handy pipe redirect that appends a datestamp to every line of output. Just paste this into
    # the top of a typical BASH script.
    # the top of a typical BASH script. Note that it outputs localtime due to limitations in
    # BASH printf (whichis used for speed reasons). Automated runner processes as a rule of thumb
    # should be set to UTC anyways, so it seems not a significant limitation.

    s_datestamp() {
    while IFS= read -r line; do
    @@ -11,7 +13,7 @@ s_datestamp() {
    if [[ "$line" =~ \[[0-9]{4}-[^[:space:]]*\] ]]; then
    echo "$line"
    else
    printf "[%(%Y-%m-%d %H:%M:%S)T] %s\n" "-1" "$line"
    printf "%(%Y-%m-%dT%H:%M:%S%z)T" "-1"
    fi
    done
    }
  2. jstine35 revised this gist Aug 24, 2024. 1 changed file with 5 additions and 7 deletions.
    12 changes: 5 additions & 7 deletions timestamp_trick.sh
    Original file line number Diff line number Diff line change
    @@ -5,15 +5,13 @@
    # the top of a typical BASH script.

    s_datestamp() {
    while read -r line; do
    timestamp=$(date -u '+%Y-%m-%d %H:%M:%S')

    while IFS= read -r line; do
    # by nature BASH might run process subst twice when using >&2 pipes. This is a lazy
    # way to avoid dumping two timestamps on the same line:
    if [[ "$line" != \[${timestamp%% *}* ]]; then
    echo "[$timestamp] $line"
    else
    if [[ "$line" =~ \[[0-9]{4}-[^[:space:]]*\] ]]; then
    echo "$line"
    else
    printf "[%(%Y-%m-%d %H:%M:%S)T] %s\n" "-1" "$line"
    fi
    done
    }
    @@ -31,7 +29,7 @@ exec 2> >(s_datestamp)
    procstarttime=$(date -u '+%s')

    s_timestamp() {
    while read -r line; do
    while IFS= read -r line; do
    curtime=$(date -u '+%s')
    deltatime=$(( curtime - procstarttime ))
    timestamp="$(printf "%03d:%02d" $(( deltatime / 60 )) $(( deltatime % 60 )))"
  3. jstine35 revised this gist Jul 27, 2022. 1 changed file with 31 additions and 0 deletions.
    31 changes: 31 additions & 0 deletions timestamp_trick.sh
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # FIRST VERSION - Outputs UTS formatted timestamp.

    # -------------------------------------------------------------------------------------
    # handy pipe redirect that appends a datestamp to every line of output. Just paste this into
    # the top of a typical BASH script.
    @@ -19,3 +21,32 @@ s_datestamp() {
    exec 1> >(s_datestamp)
    exec 2> >(s_datestamp)
    # -------------------------------------------------------------------------------------

    # SECOND VERSION - Outputs time since the script started.

    # -------------------------------------------------------------------------------------
    # handy pipe redirect that appends a datestamp to every line of output. Just paste this into
    # the top of a typical BASH script.

    procstarttime=$(date -u '+%s')

    s_timestamp() {
    while read -r line; do
    curtime=$(date -u '+%s')
    deltatime=$(( curtime - procstarttime ))
    timestamp="$(printf "%03d:%02d" $(( deltatime / 60 )) $(( deltatime % 60 )))"

    # by nature BASH might run process subst twice when using >&2 pipes. This is a lazy
    # way to avoid dumping two timestamps on the same line:
    if [[ "$line" != \[${timestamp%% *}* ]]; then
    echo "[$timestamp] $line"
    else
    echo "$line"
    fi
    done
    }

    exec 1> >(s_timestamp)
    exec 2> >(s_timestamp)
    # -------------------------------------------------------------------------------------

  4. jstine35 created this gist Jul 27, 2022.
    21 changes: 21 additions & 0 deletions timestamp_trick.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    # -------------------------------------------------------------------------------------
    # handy pipe redirect that appends a datestamp to every line of output. Just paste this into
    # the top of a typical BASH script.

    s_datestamp() {
    while read -r line; do
    timestamp=$(date -u '+%Y-%m-%d %H:%M:%S')

    # by nature BASH might run process subst twice when using >&2 pipes. This is a lazy
    # way to avoid dumping two timestamps on the same line:
    if [[ "$line" != \[${timestamp%% *}* ]]; then
    echo "[$timestamp] $line"
    else
    echo "$line"
    fi
    done
    }

    exec 1> >(s_datestamp)
    exec 2> >(s_datestamp)
    # -------------------------------------------------------------------------------------