LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Bash question (https://www.linuxquestions.org/questions/linux-newbie-8/bash-question-874484/)

TEC.Turtle 04-11-2011 09:58 PM

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

frankbell 04-11-2011 10:16 PM

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.

spazticclown 04-11-2011 10:45 PM

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.

grail 04-11-2011 10:55 PM

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


All times are GMT -5. The time now is 12:22 PM.