LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   bash script - get filename created by script using variables (http://www.linuxquestions.org/questions/linux-general-1/bash-script-get-filename-created-by-script-using-variables-770237/)

aolong 11-19-2009 11:39 AM

bash script - get filename created by script using variables
 
Probably a horrible title for this thread, but here goes...

If I create a file via bash script like so:

Code:

tar -C $BACKUP_DIR -czf backup_$HOST_$STAMP.tar.gz .
How can store the name of the created file in a variable to be used later in the script, such as echoing the name of the file created back to the user at script end?

Thank you.

Andrew

GrapefruiTgirl 11-19-2009 11:43 AM

Why not define the variable name first, before the tar command?

Code:

filename=backup_$HOST_$STAMP.tar.gz
tar -C $BACKUP_DIR -czf $filename .

..blah blah..

echo "Filename: $filename"

exit 0


aolong 11-19-2009 11:49 AM

Quote:

Originally Posted by GrapefruiTgirl (Post 3762847)
Why not define the variable name first, before the tar command?

Code:

filename=backup_$HOST_$STAMP.tar.gz
tar -C $BACKUP_DIR -czf $filename .

..blah blah..

echo "Filename: $filename"

exit 0


Because $STAMP is a timestamp, unknown until the tar command actually runs, unless I miss something...

GrapefruiTgirl 11-19-2009 11:51 AM

One of us is missing something :scratch: how about show us the chunk of code where this procedure takes place? And where the $STAMP gets defined...

aolong 11-19-2009 11:57 AM

Code:

#!/bin/bash

BACKUP_PATH=/root/tmp/
BACKUP_DIR=blah
STAMP=`date +%F_%H%M%S`
HOST=`hostname`

... lot of stuff ...

tar -C $BACKUP_DIR -czf mysqlhotcopy_$HOST_$STAMP.tar.gz .

... more stuff...

exit 0

It's probably me who's missing...

then later, I want to echo the filename that was created.

GrapefruiTgirl 11-19-2009 12:04 PM

Code:

#!/bin/bash

BACKUP_PATH=/root/tmp/
BACKUP_DIR=blah
STAMP=$(date +%F_%H%M%S)
HOST=$(hostname)

... lot of stuff ...
filename=whatever_$HOST_$STAMP.tar.gz
tar -C $BACKUP_DIR -czf $filename .

... more stuff...

echo $filename

exit 0

Should work; tune to suit your exact needs :)

Sasha

aolong 11-19-2009 12:25 PM

Perfect.

Question, was it necessary to rewrite the STAMP and HOST variables? Is there a problem with using the VAR=`command` syntax?

GrapefruiTgirl 11-19-2009 12:30 PM

using `backticks` instead of $(this) is deprecated. While either will work for simple operations, using backticks becomes confusing when there are longer commands or statements, or when you need to embed multiple instances.

I'm sure there's an "official" reason stated somewhere about why one is better than the other, but I don't know it verbatim :) though others may have more detail to provide on why to use $() instead of ``

Cheers!
Sasha

catkin 11-19-2009 01:40 PM

Quote:

Originally Posted by GrapefruiTgirl (Post 3762889)
I'm sure there's an "official" reason stated somewhere about why one is better than the other, but I don't know it verbatim :) though others may have more detail to provide on why to use $() instead of ``

Here are some reasons.


All times are GMT -5. The time now is 09:07 AM.