Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 9th, 2005, 5:04 AM   #1
raghavendra
Newbie
 
Join Date: Apr 2005
Location: Bangalore
Posts: 14
Rep Power: 0 raghavendra is on a distinguished road
Send a message via Yahoo to raghavendra
Using Date in Bash Scripting

Hi
I am trying to write a bash script crefil whose purpose is to create a file in mt home directory.........I wanted this feature to be into it.
Whenever I run the program it should create a file with a different name........say depending on the date...........if it is 9th of april 2005 then the file name should be
file090405.txt.........Is it possible to achieve?
Thanks for the help.
raghavendra is offline   Reply With Quote
Old Apr 9th, 2005, 8:20 AM   #2
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
sure, just use exec('touch `date`.txt') and use a format for date to get the right format you desire. check the man page of date
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Apr 9th, 2005, 11:30 PM   #3
raghavendra
Newbie
 
Join Date: Apr 2005
Location: Bangalore
Posts: 14
Rep Power: 0 raghavendra is on a distinguished road
Send a message via Yahoo to raghavendra
Quote:
Originally Posted by Dizzutch
sure, just use exec('touch `date`.txt') and use a format for date to get the right format you desire. check the man page of date
Hi
Thanks for the reply.........But i am getting syntax error when using it........Please can you give more details how to use it?
raghavendra is offline   Reply With Quote
Old Apr 9th, 2005, 8:26 AM   #4
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
echo $(date +%m%y%d)

I decided to do some research to learn a little on bash scripting...
__________________

tempest is offline   Reply With Quote
Old Apr 9th, 2005, 11:34 PM   #5
raghavendra
Newbie
 
Join Date: Apr 2005
Location: Bangalore
Posts: 14
Rep Power: 0 raghavendra is on a distinguished road
Send a message via Yahoo to raghavendra
Quote:
Originally Posted by tempest
echo $(date +%m%y%d)

I decided to do some research to learn a little on bash scripting...
Hi
Thanks for the reply...........But when I use this the script is creating a file by name echo How will I make it understand that is a command?
raghavendra is offline   Reply With Quote
Old Apr 9th, 2005, 9:57 AM   #6
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
right, i just forgot the format off the top of my head
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Apr 10th, 2005, 1:04 AM   #7
Dizzutch
Professional Programmer
 
Dizzutch's Avatar
 
Join Date: Dec 2004
Location: Worcester, MA
Posts: 441
Rep Power: 4 Dizzutch is on a distinguished road
Send a message via ICQ to Dizzutch Send a message via AIM to Dizzutch Send a message via MSN to Dizzutch Send a message via Yahoo to Dizzutch
#!/bin/sh
touch `date +%m%y%d`;
__________________
naked pictures of you | PFO F@H stats
Dizzutch is offline   Reply With Quote
Old Apr 10th, 2005, 1:10 AM   #8
raghavendra
Newbie
 
Join Date: Apr 2005
Location: Bangalore
Posts: 14
Rep Power: 0 raghavendra is on a distinguished road
Send a message via Yahoo to raghavendra
Hi
If I try that command in ther terminal it works.
But if I use the same thing in a bash script it does not

#!/bin/bash
/bin/tar -zcpf /home/raghu/cron/touch `date +%m%y%d`.tar.gz /home/raghu/.gaim/logs/yahoo/raghavendra83

It creates a file with name touch and not a file with the filename of date
What should I do to solve this?
raghavendra is offline   Reply With Quote
Old Apr 10th, 2005, 5:32 PM   #9
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,467
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
throw the command in a variable and execute it from the script... here is a basic example...

#!/bin/sh

MYFILE=`date +%m%y%d`;
echo $MYFILE;

DATA="this is a test";
echo "$DATA" >> $MYFILE;

MYCMD="cat $MYFILE";

$MYCMD
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 11th, 2005, 6:40 AM   #10
raghavendra
Newbie
 
Join Date: Apr 2005
Location: Bangalore
Posts: 14
Rep Power: 0 raghavendra is on a distinguished road
Send a message via Yahoo to raghavendra
Hi
Thanks for your reply. Your suggestion worked like a charm.
I did make some changes to suite my needs but it was built upon your idea
raghavendra is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:56 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC