Justin
Justin

Reputation: 2579

Include the hour in the filename for a log file

I have a ksh script where I'm creating a logFileName as follows:

logFileName=result_`date +%y%m%d_%k%M%S`.log

This results in a correct filename most of the time like the following:

result_121127_121010.log

However, at midnight it results in the following filename:

result_121127_ 01010.log

Thoughts on how to fix this? Basically, I'm looking for something other than %k to find the hour with the date command in ksh.

Upvotes: 0

Views: 743

Answers (1)

jlliagre
jlliagre

Reputation: 30823

Replace %k by %H as the former isn't padding the hour with 0 for single digit hours.

logFileName=result_`date +%y%m%d_%H%M%S`.log

or the equivalent

logFileName=result_$(date +%y%m%d_%H%M%S).log

Note that the famous Y2K bug showed it is better to use a full year like:

logFileName=result_$(date +%Y%m%d_%H%M%S).log

Upvotes: 2

Related Questions