Question

Shell script - proper use of variable in filename?

Asked by: paulc2000

Hi all,

Shell script noob alert!

I copied the code below from another site, and made a small modificiation to it - specifically the addition of the date variable.  But now it's not working...  The files it produces don't end with ".sql", ".zip" or ".tgz" as I believe they should.

So, for example, the mysqldump command produces a file called db_backup.2009-10-13_01.00.24 and no gzipped file is produced at all (because it's looking for another file with .sql at the end?) and the tar command produces a file called web_backup.2009-10-13_01.00.24

Does anyone know what I've done wrong here?  And how I can get the full script to work properly?

Thanks,

Paul

#!/bin/sh
date=`date '+%Y-%m-%d_%I.%M.%S'`
mysqldump --opt -hmysql.xxx.yyy.zzz -ppassword -uuser dbname > /dira/dirb/dirc/dird/www.test.com/db_backup.$date.sql
gzip -f /dira/dirb/dirc/dird/www.test.com/db_backup.$date.sql
tar czf /dira/dirb/dirc/dird/www.test.com/web_backup.$date.tgz /dira/dirb/dirc/dird/www.test.com/web/content/

                                  
1:
2:
3:
4:
5:

Select allOpen in new window

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-13 at 11:28:35ID24808762
Tags

variable name

,

file name

,

shell script

,

bash

,

sh

Topics

Bourne Shell (sh)

,

Bourne-Again Shell (bash)

,

Unix Systems Programming

Participating Experts
3
Points
500
Comments
32

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Decompressing a *.tgz file...
    I thought that you could decompress a *.tgz file with tar -xvzf *.tgz or with gunzip *.tgz.. Probably i remebered wrong can anyone tell me which program to use??
  2. tgz and gz
    What is the different between two files with *.tgz and also *.gz ?? How can I extract the *.tgz file ?
  3. Untar tgz file
    How do I unpack/unzip/untar a TGZ file on a Linux server? Here is the command that was used to create the file: tar cfz pbible_archive.tgz pbible www/images/seek_* www/images/blank2.gif www/pbible.html www/cgi-bin/pbible I have the tgz file now on a new server, and wish ...
  4. How do I tar and gzip a very large file?
    I have a directory called /s/mydir which contains about 30 Gigs of different files. I know that I can: $ pwd /mymount/point $ tar cvf backed /s/mydir ...and this will create a large tar file. But I also want it compressed, with gzip. So I tried: $ gzip backed.tar But it...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: amit_gPosted on 2009-10-13 at 11:38:01ID: 25562831

Change

$date

to

${date}

everywhere it is used.

 

by: paulc2000Posted on 2009-10-13 at 11:51:02ID: 25563000

Thanks.  I changed:

db_backup.$date.sql
to
db_backup.${date}.sql

and

web_backup.$date.tgz
to
web_backup.${date}.tgz

but I still ended up with

db_backup.2009-10-13_01.44.01
and
web_backup.2009-10-13_01.44.01

Is there anything else I need to do?

 

by: amit_gPosted on 2009-10-13 at 12:00:51ID: 25563116

Add

set -x

before

date=`date '+%Y-%m-%d_%I.%M.%S'`

and run it again. Post the result.

 

by: paulc2000Posted on 2009-10-13 at 12:26:03ID: 25563456

ok...  something different this time.  I changed it to

set -x date=`date '+%Y-%m-%d_%I.%M.%S'`

as you suggested (I take it I was meant to leave a space between -x and date?)

the results were:

db_backup..sql
and
web_backup..tgz

The date didn't appear in either filename, hence the 2 dots.

Any other ideas?

 

by: paulc2000Posted on 2009-10-13 at 12:27:23ID: 25563473

...and also, the sql file didn't get zipped, for some reason....

 

by: amit_gPosted on 2009-10-13 at 12:39:36ID: 25563611

Sorry I should have been more clear. The set -x has to be on its own line. Like this

set -x
date=`date '+%Y-%m-%d_%I.%M.%S'`

It would turn on debugging.

 

by: paulc2000Posted on 2009-10-13 at 12:56:34ID: 25563782

Sorry, I read you wrong.

Anyway, I put set -x in the line before, and here's the output:

backup.sh: line 2: set: -
: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]


...sorry this is taking so long!  Do I need to write set --x instead of set -x?

 

by: amit_gPosted on 2009-10-13 at 13:13:41ID: 25564005

What Unix is this? Can you try

set -o xtrace

 

by: paulc2000Posted on 2009-10-13 at 13:25:15ID: 25564141

I'm using Rackspace Cloud.  I ran uname and got:

2.6.18-128.2.1.el5

set -o xtrace produced this:

backup.sh: line 2: set: xtrace
: invalid option name

 

by: amit_gPosted on 2009-10-13 at 13:47:43ID: 25564404

Try

uname -a

Also add

echo /dira/dirb/dirc/dird/www.test.com/db_backup.$date.sql
echo /dira/dirb/dirc/dird/www.test.com/db_backup.$date.sql
echo /dira/dirb/dirc/dird/www.test.com/web_backup.$date.tgz

and see if all these are correct.

 

by: paulc2000Posted on 2009-10-13 at 14:11:07ID: 25564761

I think it's getting worse...  this is the output now:

  Command to be executed is:  /usr/bin/perl /a/b/c/d/www.test.com/backup.sh
Where should log be written:  /a/b/c/d/www.test.com/logs
 
/a/b/c/d/www.test.com/backup.sh: line 2: set: xtrace
: invalid option name
uname: invalid option --
Try `uname --help' for more information.
tar: Removing leading `/' from member names
tar: /a/b/c/d/www.test.com/web/content/\r: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors
/a/b/c/d/www.test.com/db_backup.2009-10-13_04.06.01
.sql
/a/b/c/d/www.test.com/db_backup.2009-10-13_04.06.01
.sql
/a/b/c/d/www.test.com/web_backup.2009-10-13_04.06.01
.tgz

 

by: TintinPosted on 2009-10-13 at 14:31:00ID: 25564982

If your original script didn't create the files with the extensions, then there is something, very, very strange with your system.  There is no logical reason why I shouldn't have run correctly.

Please post the complete modified script you now have.  It looks like you must have added a bunch of lines that shouldn't be in there.

 

by: amit_gPosted on 2009-10-13 at 14:51:00ID: 25565180

set -x is not working for you so it should be taken out. uname -a was not supposed to be added to the script. I am posting the exact script that you should have.

Also, how are you executing these? Do you have shell access or using some other tool?

#!/bin/sh
 
date=`date '+%Y-%m-%d_%I.%M.%S'`
 
echo "Debugging statements begin"
echo /dira/dirb/dirc/dird/www.test.com/db_backup.${date}.sql
echo /dira/dirb/dirc/dird/www.test.com/db_backup.${date}.sql
echo /dira/dirb/dirc/dird/www.test.com/web_backup.${date}.tgz
echo "Debugging statements end"
echo
 
mysqldump --opt -hmysql.xxx.yyy.zzz -ppassword -uuser dbname > /dira/dirb/dirc/dird/www.test.com/db_backup.${date}.sql
gzip -f /dira/dirb/dirc/dird/www.test.com/db_backup.${date}.sql
tar czf /dira/dirb/dirc/dird/www.test.com/web_backup.${date}.tgz /dira/dirb/dirc/dird/www.test.com/web/content/
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:

Select allOpen in new window

 

by: paulc2000Posted on 2009-10-13 at 15:42:51ID: 25565561

ok, I ran amit_g's script.  Here's the result:

(I've added "blah" and "test.com" as I don't want to publish this info, sorry if I'm paranoid)

  Command to be executed is:  /usr/bin/perl /blah/www.test.com/backup.sh
Where should log be written:  /blah/www.test.com/logs
 
/blah/www.test.com/backup.sh: line 2:
: command not found
/blah/www.test.com/backup.sh: line 4:
: command not found
Debugging statements begin
/blah/www.test.com/db_backup.2009-10-13_05.31.01
.sql
/blah/www.test.com/db_backup.2009-10-13_05.31.01
.sql
/blah/www.test.com/web_backup.2009-10-13_05.31.01
.tgz
Debugging statements end
/blah/www.test.com/backup.sh: line 10: echo
: command not found
/blah/www.test.com/backup.sh: line 11:
: command not found
tar: Removing leading `/' from member names


This script is being run as a cron job.  If normal shell access is available to me, I don't know how to get to it.

Maybe Rackspace supplies only a trimmed back shell version, but the original script I posted (without the date modification) was taken from their site.

If you guys think there's something wrong with the system, I'm happy to close this question and award points for your efforts.

Let me know.

 

by: amit_gPosted on 2009-10-13 at 15:58:27ID: 25565689

>> /usr/bin/perl /blah/www.test.com/backup.sh

It seems to be executed using perl but the script itself is not a perl script. So may be there is a translation script in between. Even the blank lines are not allowed (throwing errors).

Can you check the following script (create a new one to test)?

#!/bin/sh 
date=$(date '+%Y-%m-%d_%I.%M.%S')
echo "Debugging statements begin"
echo /dira/dirb/dirc/dird/www.test.com/db_backup.${date}.sql
echo /dira/dirb/dirc/dird/www.test.com/db_backup.${date}.sql
echo /dira/dirb/dirc/dird/www.test.com/web_backup.${date}.tgz
echo "Debugging statements end"
                                              
1:
2:
3:
4:
5:
6:
7:

Select allOpen in new window

 

by: paulc2000Posted on 2009-10-13 at 16:11:38ID: 25565790

this is the result:

  Command to be executed is:  /blah/www.test.com/bak2.sh
Where should log be written:  /blah/www.test.com/logs
 
Debugging statements begin
/blah/www.test.com/db_backup.2009-10-13_06.03.01
.sql
/blah/www.test.com/db_backup.2009-10-13_06.03.01
.sql
/blah/www.test.com/web_backup.2009-10-13_06.03.01
.tgz
Debugging statements end

 

by: ozoPosted on 2009-10-13 at 16:21:34ID: 25565856

can you try
date=`date '+%Y-%m-%d_%I.%M.%S' | tr -cd '!-_'`
and see if that makes a difference?

 

by: paulc2000Posted on 2009-10-13 at 16:44:53ID: 25565969

@ozo: same result!  what are you expecting to see?

 

by: amit_gPosted on 2009-10-13 at 21:46:37ID: 25567321

There is a weird newline char in before extensions. If you executed this script in any flavor of Unix, it won't happen. Can you try this one?

#!/bin/sh
date=$(date '+%Y-%m-%d_%I.%M.%S.sql')
echo "Debugging statements begin"
echo /dira/dirb/dirc/dird/www.test.com/db_backup.${date}
echo /dira/dirb/dirc/dird/www.test.com/db_backup.${date}
echo "Debugging statements end"

 

by: paulc2000Posted on 2009-10-14 at 01:37:32ID: 25568283

and here's the result:

  Command to be executed is:  /usr/bin/perl /blah/www.test.com/backup.sh
Where should log be written:  /blah/www.test.com/logs
 
Debugging statements begin
/blah/www.test.com/db_backup.2009-10-14_03.34.01.sql /blah/www.test.com/db_backup.2009-10-14_03.34.01.sql Debugging statements end

 

by: TintinPosted on 2009-10-14 at 02:15:33ID: 25568457

On your original script, can you please post the output of

od -c scriptname

 

by: paulc2000Posted on 2009-10-14 at 03:49:22ID: 25568970

I keep getting a carriage return char in the script, which messes it all up, even though I've stripped them all out with Notepad.

Guys,  I appreciate all your efforts, but I think I'll have to look at another solution, perhaps PHP.

Please tell me how I should proceed with closing this question?  I'm happy to award the points for the effort, but if this is against protocol, please let me know.

 

by: amit_gPosted on 2009-10-14 at 07:52:01ID: 25571164

The last test seems to be ok

/blah/www.test.com/db_backup.2009-10-14_03.34.01.sql

so we can try it. Could you try the below script?

#!/bin/sh
sqlFilename=`date '+%Y-%m-%d_%I.%M.%S.sql'`
tarFilename=`date '+%Y-%m-%d_%I.%M.%S.tgz'`
mysqldump --opt -hmysql.xxx.yyy.zzz -ppassword -uuser dbname > /dira/dirb/dirc/dird/www.test.com/db_backup.$sqlFilename
gzip -f /dira/dirb/dirc/dird/www.test.com/db_backup.$sqlFilename
tar czf /dira/dirb/dirc/dird/www.test.com/web_backup.$tarFilename /dira/dirb/dirc/dird/www.test.com/web/content/

                                              
1:
2:
3:
4:
5:
6:

Select allOpen in new window

 

by: paulc2000Posted on 2009-10-14 at 08:20:48ID: 25571563

so close!

I got:

web_backup.2009-10-14_10.13.01.tgz (doesn't seem to have actually compressed the files, but maybe that's just a switch I need on tar)

and

db_backup.2009-10-14_10.13.01.sql (but no zip....)

the output in the log all looks clean though:

  Command to be executed is:  /usr/bin/perl /blah/www.test.com/backup2.sh
Where should log be written:  /blah/www.test.com/logs
 
tar: Removing leading `/' from member names


What file should the gzip command produce?  I thought it would give me db_backup.$sqlFilename.zip or db_backup.$sqlFilename.gz?

 

by: amit_gPosted on 2009-10-14 at 08:38:59ID: 25571798

It should have created

db_backup.2009-10-14_10.13.01.sql
db_backup.2009-10-14_10.13.01.sql.gz
db_backup.2009-10-14_10.13.01.tgz

The time stamp in the filename on tgz may be different than timestamp on .sql.

Does the log file /blah/www.test.com/logs contain anything?

 

by: paulc2000Posted on 2009-10-14 at 08:46:30ID: 25571882

nothing in the log except what I wrote above:

Command to be executed is:  /usr/bin/perl /blah/www.test.com/backup2.sh
Where should log be written:  /blah/www.test.com/logs
 
tar: Removing leading `/' from member names


The timestamps are 2 mins apart, but I'm afraid there's still no db_backup.2009-10-14_10.13.01.tgz.  Only db_backup.2009-10-14_10.13.01.sql is there.

 

by: amit_gPosted on 2009-10-14 at 09:49:03ID: 25572524

Are you able to run commands directly? If so, could you run this

gzip -f /dira/dirb/dirc/dird/www.test.com/db_backup.2009-10-14_10.13.01.sql

and see if it runs fine and produces zipped file or throws some error.

 

by: paulc2000Posted on 2009-10-14 at 10:42:23ID: 25573032

I'm afraid I don't have direct shell access.  I checked again to be sure.

Any last ideas?  You've already got the points :)

 

by: amit_gPosted on 2009-10-14 at 11:54:48ID: 25573786

Create a standalone script and run it. If it doesn't create the zipped file, ask for support from your provider. Make sure that the file you are using db_backup.2009-10-14_10.13.01.sql exists and the whole path is correct. Then they should be able to tell you why it is not working and may be there is another way to do it.

#!/bin/sh
gzip -f /dira/dirb/dirc/dird/www.test.com/db_backup.2009-10-14_10.13.01.sql
                                              
1:
2:

Select allOpen in new window

 

by: paulc2000Posted on 2009-10-18 at 14:58:11ID: 31640698

It seems the problem is compunded by the system I'm using so the solutions offered didn't work with my particular setup, but presumably would work on another system.

 

by: paulc2000Posted on 2009-10-28 at 04:01:55ID: 25681803

So, I found the problem - or what I think the problem is.  The filenames that are being produced by the script include one or more carriage returns.  I had been using FileZilla for FTP, and this was not apparent when viewing the contents of the dir.  Additionally, I was unable to modify (rename, delete, download, etc) any of the files produced by the script.  I switched to another FTP client (WinSCP) and immediately I could see the extra chars in the filenames.

However, I still have the problem!  I've tried several things to remove the CRs from the script, but to no avail.  I backspaced to the line above, then hit return; I viewed the script in a hex editor; saving the file as ANSI every time.  But still it's producing these screwed up filenames (also, the SQL file IS being gzipped after all, but FileZilla didn't show the correct name).  The ACTUAL filenames are:

db_backup.2009-10-28_04.49.01.sqlCRCR.gz (shown in FZ as db_backup.2009-10-28_04.49.01.sql)

and

web_backup.2009-10-28_04.49.02.tgzCR (shown in FZ as web_backup.2009-10-28_04.49.02.tgz)

where CR is the previously-invisible carriage return char.  Looking back at my posts above, I should have recognised the problem from the \r outputs and that the names of the files were wrapped, eg:

tar: /a/b/c/d/www.test.com/web/content/\r: Cannot stat: No such file or directory
tar: Error exit delayed from previous errors
/a/b/c/d/www.test.com/db_backup.2009-10-13_04.06.01
.sql
/a/b/c/d/www.test.com/db_backup.2009-10-13_04.06.01
.sql
/a/b/c/d/www.test.com/web_backup.2009-10-13_04.06.01
.tgz

So, my next question is, how do I get rid of these horrible CRs?!  If I should open a new question, please let me know.

Thanks,

Paul

 

by: paulc2000Posted on 2009-10-28 at 04:30:56ID: 25682037

ok.......  I used ANOTHER hex editor, and it succeeded in removing the CRs...  it works now..... :)

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...