Results 1 to 7 of 7

Thread: Shell script timestamp variable

  1. #1
    Join Date
    Feb 2013
    Beans
    104

    Shell script timestamp variable

    I'm no linux expert so I was hoping someone could help me on this shell script issue with a timestamp variable.

    This is the line I have in use. It is a line to insert into my logfile for the shell script. I am noticing that the date does not update when I reuse the variable LOGENTRY. How can I get this variable to update the timestamp?

    Code:
    LOGENTRY="`date '+%b %d %k:%M:%S'` `hostname -s` `basename $0`:"
    echo $LOGENTRY  something to log

    Thanks in advance!

  2. #2
    Join Date
    Oct 2014
    Beans
    19
    Distro
    Ubuntu Studio 14.04 Trusty Tahr

    Re: Help with shell script timestamp variable

    Make a loop and delay by using "sleep 1".
    Ex.
    Code:
    while true; do
        LOGENTRY="`date '+%b %d %k:%M:%S'` `hostname -s` `basename $0`:"
        echo $LOGENTRY  something to log
        sleep 1
    done
    There are better ways to do it, but this is one way to.
    Hope this helps!

  3. #3
    Join Date
    Sep 2006
    Beans
    8,627
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Help with shell script timestamp variable

    I'm pretty sure that there is not a way to use a regular environment variable in that way. I think you'd have to put it on a single line:

    Code:
    echo "$(/bin/date +'%b %d %k:%M:%S') $(/bin/hostname -s) $(/usr/bin/basename -- "$0"):" "something to log"

  4. #4
    Join Date
    Feb 2013
    Beans
    104

    Re: Help with shell script timestamp variable

    Rats! I have been playing with it since posting and discovered that by eliminating the VERY helpful variable, it was able to update.

    I just figured linux is so powerful, why can't a variable actively evaluate again and again by default?

    Is there some way to force evaluation? I've seen eval?

  5. #5
    Join Date
    Jul 2013
    Location
    Wisconsin
    Beans
    4,952

    Re: Shell script timestamp variable

    It's just a variable - an inert set of data bytes. It's not a class or a function or a method or anything active.

    You can write a function to update the variable if you really want to.

    Or you can write a function to create an updated timestamp string each time you need to log something.

  6. #6
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    2,172
    Distro
    Ubuntu 16.04 Xenial Xerus

    Re: Shell script timestamp variable

    What you can do is keep the format in a variable to make sure that the same date format is used consistently. But considering that all logging goes also to the same file, if you want to make your logging simpler you write a function:

    Code:
    function log {
            printf "%s (%06d): %s\n" "$(date '+%F %H:%M:%S')" $$ "$*" >> /some/path/to/file.log
    }
    and in your code
    Code:
    log "this line will be logged"
    log this line will be logged too
    Warning: unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.

  7. #7
    Join Date
    Feb 2013
    Beans
    104

    Re: Shell script timestamp variable

    @ofnuts - I thought I submitted my response, but I guess I missed that. I went with middle ground, but I will look to implement a simple function like you proposed. Its not painful. Thanks!

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •