LinuxQuestions.org
Help answer threads with 0 replies.
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 04-11-2011, 10:58 PM   #1
TEC.Turtle
LQ Newbie
 
Registered: Jan 2011
Posts: 12

Rep: Reputation: 0
Bash question


I'm trying to put the date in the name of my file but cant seem to get it I just end up with the word date in the file instead of the date. my script looks like this.
#!/bin/bash
x="/var/log/system/"
y=date
z=${y:11:8}
top > $x$y.txt
 
Old 04-11-2011, 11:16 PM   #2
frankbell
LQ Guru
 
Registered: Jan 2006
Location: Virginia, USA
Distribution: Slackware, Debian, Mint, OpenBSD
Posts: 11,333
Blog Entries: 12

Rep: Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729Reputation: 2729
I think your variable declaration needs some work.

Here's a snippet from a script I wrote to backup my blog database. First, I created a directory to hold the backup, then I ran mysqldump and dumped the backup into that variable.

I am not a coder and I'm sure there was a better way, but this worked. In this case, I wanted the hour and minute in the directory name.

Code:
#Set the variable DATE

DATE=1

#Define now as today’s day-month-year-hour-minute.

DATE=$(date +%m-%d-%Y-%H-%M)

#Make the directory to hold the backup file.

mkdir /root/backups/sql/$DATE
See this: http://tldp.org/LDP/Bash-Beginners-G...ners-Guide.pdf. It's excellent.
 
Old 04-11-2011, 11:45 PM   #3
spazticclown
Member
 
Registered: Sep 2010
Distribution: Fedora, Android, CentOS
Posts: 91
Blog Entries: 2

Rep: Reputation: 21
Check out the man page for date
Code:
#! /bin/bash
pre="/var/log/system/"
post=`date '+%Y:%m:%d'`
fname="description-"
ext=".txt"
echo "$pre$fname$post$ext"
Output:
Code:
/var/log/system/description-2011:4:11.txt
Give this a try and alter as necessary.
 
Old 04-11-2011, 11:55 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,245

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
So the examples provided by other's are where you need to go, but in relation to your own script lines, the following is your error:
Code:
y=date  # this says to set y equal to the string 'date'

y=$(date)  # whereas this says to execute the command (could be a function of your own as well) and assign the value returned to y
Hope that helps
 
  


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
A question about BASH operand Linux - Newbie 2 11-27-2005 10:49 PM
Bash question lnxduck Programming 3 09-30-2005 09:50 AM
bash question? shanenin Linux - Software 1 11-26-2004 01:22 PM
Bash question Warmduvet Programming 4 09-24-2004 09:19 PM
bash question rjcrews Linux - Newbie 2 08-04-2004 10:46 PM


All times are GMT -5. The time now is 08:01 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