LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-19-2009, 10:39 AM   #1
aolong
Member
 
Registered: Aug 2006
Location: US
Distribution: Debian, CentOS
Posts: 40

Rep: Reputation: 15
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
 
Old 11-19-2009, 10:43 AM   #2
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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
 
Old 11-19-2009, 10:49 AM   #3
aolong
Member
 
Registered: Aug 2006
Location: US
Distribution: Debian, CentOS
Posts: 40

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by GrapefruiTgirl View Post
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...
 
Old 11-19-2009, 10:51 AM   #4
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
One of us is missing something how about show us the chunk of code where this procedure takes place? And where the $STAMP gets defined...
 
Old 11-19-2009, 10:57 AM   #5
aolong
Member
 
Registered: Aug 2006
Location: US
Distribution: Debian, CentOS
Posts: 40

Original Poster
Rep: Reputation: 15
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.
 
Old 11-19-2009, 11:04 AM   #6
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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
 
Old 11-19-2009, 11:25 AM   #7
aolong
Member
 
Registered: Aug 2006
Location: US
Distribution: Debian, CentOS
Posts: 40

Original Poster
Rep: Reputation: 15
Perfect.

Question, was it necessary to rewrite the STAMP and HOST variables? Is there a problem with using the VAR=`command` syntax?
 
Old 11-19-2009, 11:30 AM   #8
GrapefruiTgirl
Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550Reputation: 550
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
 
Old 11-19-2009, 12:40 PM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by GrapefruiTgirl View Post
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.
 
  


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] bash script - return full path and filename Andy Alkaline Programming 44 01-10-2013 09:11 AM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
changing filename using Bash Script tmbigrigg Programming 10 11-10-2007 07:02 AM
Getting the first part of a filename in a BASH script trevelluk Programming 3 02-15-2005 01:06 AM
bash script - incrementing a filename in a script tslinux Programming 10 08-05-2003 11:58 PM


All times are GMT -5. The time now is 06:38 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration