Click to See Complete Forum and Search --> : Using 'date' to create file name?


Cadillac84
08-26-2002, 01:33 AM
I am logging events continuously and want to start a new log each day using the date as the file name.

At a shell prompt, I can obtain the desired filename thusly:

DATE='date +%y%m%dxyz.log'

$DATE will print 020826xvz.log

That ("020826xyz.log") is what I'd like my filename to be.

If I set the string DATE explicitly such as:

DATE='020826xyz.log'

then I can do things like

ls -l > $DATE

and I get a file with the desired name.

However, if I try to set the date using the 'date' function, then when I try to create the file, I get an error message:

"bash: $DATE: ambiguous redirect

The end result desired is to log activity for a day saving the results to a file named for that date. At the end of the day, I would stop logging, determine the new date and begin a new log using the then-current date as the file name.

Can someone explain to me where I have wandered off the path?

Thanks in advance.

Chuck Moore
CNMoore@Knology.net

bwkaz
08-26-2002, 07:08 AM
Try process substitution:

DATE=$(date +%y%m%d)xyz.log

ls -l >$DATEWhat it does is run the "date" command in a subshell, and puts its output in where the command appears. So if the date is 020826, then you get 020826xyz.log.

Cadillac84
08-26-2002, 07:58 AM
That works just exactly as I hoped.

That plus a "sleep 60" to make sure the date changes while the script is running, and my new log will start automatically. It'll actually stop logging for that one minute, but I'll be doing it at 23:59 when I really don't need to be logging anyway.


I'm very happy!

Chuck :-)