LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-19-2008, 09:27 AM   #1
runnerpaul
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Fedora 5
Posts: 63

Rep: Reputation: 15
Shell Script - Date/Time


Hi,

In shell scripting how would I add the date/time to a log file?

Cheers
Paul
 
Old 08-19-2008, 09:31 AM   #2
Agrouf
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: LFS
Posts: 1,591

Rep: Reputation: 79
Do you mean 'add' as 'append the date at the end of a log file'?
If that is so, do that:
Quote:
date >> file.log
 
Old 08-19-2008, 09:34 AM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
If you want to put the date in the log filename, use command substitution. For example
Code:
command >> /path/to/$(date +%Y%m%d)_file.log
will append the output of the command to /path/to/20080819_file.log.
 
Old 08-19-2008, 09:39 AM   #4
b0uncer
Guru
 
Registered: Aug 2003
Distribution: CentOS, OS X
Posts: 5,131

Rep: Reputation: Disabled
Also take a look at 'date' command's man page:
Code:
man date
(or see this page instead)

Notice the '+FORMAT' there? See the lower part of the man page for formatting strings and combine them into the command to be able to append the date/time in the format you want/need (the default format given by plain 'date' might not be what you want).

That method, redirecting the output of a command to a file using '>>', is fairly common and works in DOS too, exactly the same way (except that there might not be 'date' command available). Two greater than -chars ('>>') means 'append' while one ('>') alone clears the file contents before writing the new content, so be aware:
Code:
date >> file.log  #existing content remains, date is added to the end
date > file.log  #existing content vanishes, now file only contains date's output
For more information about what you can do with your shell ('tricks' if you like), see your shell's man page, for example
Code:
man bash
EDIT: of course that means the shell you use to run your script; you should define it in the first line of the script file, for example
Code:
#!/bin/bash

Last edited by b0uncer; 08-19-2008 at 09:41 AM.
 
Old 08-19-2008, 12:10 PM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I usually write a function for writing to log files, something like this:

Code:
#!/bin/bash

scriptname=${0##*/}
logfile="whatever.log"

logit () {
    echo "$(date +%Y%m%d-%T) $scriptname : $@" >> "$logfile"
}

logit "starting script"

...

logit "ending script"
The log file will then contain lines formatted like this:
Code:
20080819-17:09:49 myscript : starting script
20080819-17:09:50 myscript : ending script
 
Old 08-20-2008, 04:20 AM   #6
runnerpaul
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Fedora 5
Posts: 63

Original Poster
Rep: Reputation: 15
Hi I tried the following:

Code:
#!/sbin/sh -
log() {
    /usr/bin/echo "$(date +%Y%m%d-%T) $1" >> ${LOGFILE}
}
But in mylogs instead of printing the date/time I got the following:

Code:
$(date +%Y%m%d-%T)
 
Old 08-20-2008, 05:43 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
I can think of two possible reasons:
  1. Although you have put double quotes in your post, maybe you used single quotes in your script, like this:
    Code:
    /usr/bin/echo '$(date +%Y%m%d-%T) $1' >> ${LOGFILE}
    Single quotes won't work, since they mean that the contents of the quotes are a literal string, and no expansion of the $( ... ) will be done.
  2. You are using a strange shell which does not expand $( ... ). What is /sbin/sh? If you do not use bash, you should explicitly state what you are using in posts to LQ - most people here will assume you are using bash unless you tell them otherwise (me included).
 
Old 08-20-2008, 05:47 AM   #8
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
From your sha-bang it seems you're using the Bourne shell, where the syntax $(command) for command substitution is not recognized, since it is specific to bash. Use back ticks instead:
Code:
/usr/bin/echo "`date +%Y%m%d-%T` $1" >> ${LOGFILE}
Edit: matthew, you were faster than me. You're right, it is a strange location for the shell executable, anyway if the shell does not complain about it, the sha-bang is correct.

Last edited by colucix; 08-20-2008 at 05:56 AM.
 
Old 08-20-2008, 06:34 AM   #9
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
For the record, both backtick execution
Code:
`command`
and $(command) execution
Code:
$(command)
are different notations to achieve more or less the same thing in bash and many other borne shell derivatives (including zsh, ksh and dash). i.e. "execute a command, and put the output of the command (from standard output) here".

In all these shells you can also use the `backtick` notation. I prefer not to instruct people using backticks for two reasons:
  • They are extremely easy to confuse with single quotes, and often lead to a lot of back and forth because the font which is used to display the message either doesn't discriminate at all between the two characters, or the reader does not pay close enough attention.
  • Backtick execution is not nestable. Using $(...), you can have an "inner" execution, which is done first. This is not such a huge reason... it is not a commonly used feature, although I will say that I have used it in the past. Sure enough it's possible to do the same thing in two stages with backticks, but sometimes it's nice to be able to nest these things.
 
Old 08-20-2008, 08:45 AM   #10
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I would add that the syntax $(command) interprets the double backslash correctly, or at least in the way you expect it, as an escaped backslash.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script to remove old files based on date WindozBytes Linux - General 12 06-04-2012 02:21 AM
shell script to find modified date and last accessed date of any file. parasdua Linux - Newbie 6 04-22-2008 10:59 AM
Shell Script for Unix Date ??? ajeetraina Linux - Newbie 11 02-08-2008 01:58 AM
Shell Script.. Date format handling C-RAF. Programming 2 02-14-2006 09:34 AM
Shell Script for Gzipping with date output Junior41180 Programming 1 10-16-2004 07:39 AM


All times are GMT -5. The time now is 07:00 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration