LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-16-2012, 05:56 AM   #1
Springs
Member
 
Registered: Apr 2008
Posts: 63

Rep: Reputation: 0
create a file with the date included.


Hi all..

trying to create a script that creates a file which outputs the date in the file name

Code:
LOG='/root/scripts/logs/file_move-$DATE'
#DATE=`date '+%d-%m-%y'`
#DATE=$( date '+%Y-%m-%d_%H-%M' )

touch $LOG
My problem is that the output creates a file called "file_move-$DATE" and not of the actual date.

Although there hashed out I've tried both Date commands.

Any ideas where I'm going wrong?
 
Old 11-16-2012, 06:10 AM   #2
jsaravana87
Member
 
Registered: Aug 2011
Location: Chennai,India
Distribution: Redhat,Centos,Ubuntu,Dedian
Posts: 558
Blog Entries: 5

Rep: Reputation: Disabled
#!/bin/bash

#Script -Purpose:Backup script
# Exits with zero if no error.
# Step -1 Function to archieve file using date and time
date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`
# Step -2 Function to create file on /root/scripts/logs with Timestamp
touch /root/scripts/logs/file_move-$date
 
Old 11-16-2012, 07:05 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,349

Rep: Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750Reputation: 2750
do not use ' . use " instead :
LOG="/root/scripts/logs/file_move-$DATE"
 
Old 11-21-2012, 05:15 AM   #4
Springs
Member
 
Registered: Apr 2008
Posts: 63

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by arun5002 View Post
#!/bin/bash

#Script -Purpose:Backup script
# Exits with zero if no error.
# Step -1 Function to archieve file using date and time
date=`/bin/date "+%Y.%m.%d.%H.%M.%S"`
# Step -2 Function to create file on /root/scripts/logs with Timestamp
touch /root/scripts/logs/file_move-$date
Thanks, this has worked for me.

The next issue i have is the output to the file won't go into the saved file.

Code:
LOG="/root/scripts/logs/file_move-$date"

date=`/bin/date "+%d.%m.%Y.%H.%M"`

touch /root/scripts/logs/file_move-$date

echo "Start of Log for $(date)" >> $LOG
Any further ideas?
 
Old 11-21-2012, 05:24 AM   #5
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,452

Rep: Reputation: 85
Code:
date=`/bin/date "+%d.%m.%Y.%H.%M"`
LOG="/root/scripts/logs/file_move-$date"
touch /root/scripts/logs/file_move-$date
echo "Start of Log for $(date)" >> $LOG

Set the date function first before you use it in the next line. Otherwise $date has nothing and output is sent to

/root/scripts/logs/file_move-

Last edited by fakie_flip; 11-21-2012 at 05:26 AM.
 
1 members found this post helpful.
Old 11-21-2012, 05:33 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,362

Rep: Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377Reputation: 2377
The problem is you've used the value for $date at the first line
Code:
LOG="/root/scripts/logs/file_move-$date"
but at this point there is no value assigned to $date
Try this to see
Code:
set -xv
LOG="/root/scripts/logs/file_move-$date"

date=`/bin/date "+%d.%m.%Y.%H.%M"`

touch /root/scripts/logs/file_move-$date

echo "Start of Log for $(date)" >> $LOG
 
Old 11-21-2012, 05:41 AM   #7
Springs
Member
 
Registered: Apr 2008
Posts: 63

Original Poster
Rep: Reputation: 0
thanks. Solved that issue as well.

Will have to remember to call the date first.

Thanks Guys
 
Old 11-21-2012, 05:54 AM   #8
mandyapenguin
Member
 
Registered: Nov 2011
Location: India
Distribution: RedHat, Cent OS, Fedora, Debian, Ubuntu
Posts: 106

Rep: Reputation: Disabled
Quote:
The next issue i have is the output to the file won't go into the saved file.

Any further ideas?
Code:
# cat script 
#!/bin/bash
DATE="$(date '+%d-%b-%Y')"
LOG="/root/scripts/logs/file_move-$DATE"
echo "Start of Log for $(date)" >> $LOG
cat $LOG
 
Old 11-21-2012, 06:00 AM   #9
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,452

Rep: Reputation: 85
Also you want to not name your variables the same as commands. date is used as in your script as both a date and variable. current_date would be better. Avoid using all caps. These are for environment variables to use unless you're intentionally modifying an environment variable.
 
Old 11-21-2012, 09:44 PM   #10
jsaravana87
Member
 
Registered: Aug 2011
Location: Chennai,India
Distribution: Redhat,Centos,Ubuntu,Dedian
Posts: 558
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
The next issue i have is the output to the file won't go into the saved file.

Any further ideas?
# Function to Print the logs Backup status with current date

echo "$(date) Backup taken successsfully ">>/root/logs/logbackup-status-$date.log
 
Old 11-27-2012, 02:12 AM   #11
fakie_flip
Senior Member
 
Registered: Feb 2005
Location: San Antonio, Texas
Distribution: Gentoo Hardened using OpenRC not Systemd
Posts: 1,452

Rep: Reputation: 85
I'm not sure why you'd want a separate file for each date that also has the date in it, seems redundant but ok.

Code:
[root@beastlinux ~]# mkdir -p /root/scripts/logs
[root@beastlinux ~]# vim backup.sh
[root@beastlinux ~]# ./backup.sh
Start of Log for 27-Nov-2012
[root@beastlinux ~]# date 112800002012
Wed Nov 28 00:00:00 CST 2012
[root@beastlinux ~]# ./backup.sh
Start of Log for 28-Nov-2012
[root@beastlinux ~]# ntpdate us.pool.ntp.org
27 Nov 02:07:58 ntpdate[9512]: step time server 216.37.64.3 offset -79161.335667 sec
[root@beastlinux ~]# cd scripts/logs/
[root@beastlinux logs]# ls
file_move-27-Nov-2012  file_move-28-Nov-2012
[root@beastlinux logs]# cat file_move-27-Nov-2012 
Start of Log for 27-Nov-2012
[root@beastlinux logs]# cat file_move-28-Nov-2012 
Start of Log for 28-Nov-2012
[root@beastlinux logs]#
The script I used:

Code:
#!/bin/bash
current_date="$(date '+%d-%b-%Y')"
log="/root/scripts/logs/file_move-$current_date"
echo "Start of Log for $current_date" | tee -a "$log"
Again, using date as a variable can lead to problems as it's also a command and can cause confusion to someone reading your code.

Unless you are intentionally accessing or modifying an environment variable, don't use all CAPS in your variable names.

By convention, environment variables (PATH, EDITOR, SHELL, ...) and internal shell variables
(BASH_VERSION, RANDOM, ...) are fully capitalized. All other variable names should be
lowercase. Since variable names are case-sensitive, this convention avoids accidentally
overriding environmental and internal variables.

Last edited by fakie_flip; 11-27-2012 at 02:18 AM.
 
  


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
[SOLVED] BASH: Keep original mod date of a file and 'date' it back to same file? SilversleevesX Programming 4 07-16-2010 11:12 AM
[SOLVED] Use date with touch to create new file with date based name craigjl Linux - Newbie 5 03-12-2010 09:46 AM
Create a script that asks the user for a file and then appends the date to the file larry5757 Linux - Newbie 2 12-04-2009 06:33 AM
Bash script to read file and alter lines depending on create date ChristianHein Linux - General 13 08-04-2007 05:39 AM
howto create a file based on date in bash rohan208 Linux - Newbie 2 05-07-2004 03:54 PM


All times are GMT -5. The time now is 06:44 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration