Click to See Complete Forum and Search --> : using date as a output filename in a script!!!
gsanand
09-20-2002, 02:54 PM
How can I use a date function in a script, so every day when the script runs, it makes a new file with current date as its filename, so i have everydays logs to look at by date.
In other words, a have a script that runs and shows output. I have redirected this output to a file. How and what variable can i use in the script so the filename changes everyday.
z0mbix
09-20-2002, 03:27 PM
touch file`date '+%m%d%y'`.tar.gz
This what you are looking for?
Lorithar
09-20-2002, 03:29 PM
try this
file_date=`date +"%d%m%y" `
put ddmmyy in the param file_date
as in 200902 for 20th sept 2002
gsanand
09-20-2002, 04:52 PM
both the above solutions does not seem to work. It works when i just do
date +'%y%m%d'
but it does not works thru the script.
Please advice!!
WakeBdr
09-20-2002, 04:59 PM
What shell are you scripting in?
gsanand
09-20-2002, 05:08 PM
im not very much familiar with Unix-Linux systems. But i think its borne shell as it shows $prompt.
WakeBdr
09-20-2002, 05:17 PM
Because you say you are redirecting your output, it sounds like you are running something like
$prompt: scriptname > date
It would be easier to redirect your output in the script itself instead of on the command line.
All you would need to do is get your filename by way of the date function posted earlier, then everytime you echo something, echo it to the file.
outputFile=`/bin/date +%m%d%Y`
echo "output" > $outputFile
gsanand
09-20-2002, 05:42 PM
Actually I use crotab to backup data. Now the command I run in crontab shows the list of files being backed up. Now if I use /home/gsanand/runtape >> tape_log.txt
I would live to put a date such as tape_log<date>.txt, so the logs dont get over written.
Please help!!
WakeBdr
09-20-2002, 05:59 PM
Can you post your runtape script?
gsanand
09-20-2002, 06:10 PM
tar cvf /dev/st0 /home/gsanand/ /public/
WakeBdr
09-20-2002, 07:56 PM
Try this
#!/bin/sh
output=`/bin/date +%m%d%Y`".tar"
/bin/tar cvf $output /home/gsanand/ /public/
That should tar up /home/gsanand/ and /public/ into 09202002.tar for today.