LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-07-2012, 06:30 AM   #1
remedia
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Rep: Reputation: Disabled
Variable stops file extension being included in filename


The script below has 2 versions one works and one does not, I have even re-saved the one that works as the one that does not and it still produces the same error. So in essence it was the same script even writing to the WORKING directory but the issue still persists.

WORKING
Code:
#!/bin/bash
NOW=$(date +"%b-%d-%y")
mysqldump -h server -u user -ppassword dbname | gzip > /path/to/folder/db-backups/db-$NOW.sql.gz
NOT WORKING
Code:
#!/bin/bash
NOW=$(date +"%b-%d-%y-%H-%M")
mysqldump -h server -u user -ppassword dbname | gzip > /path/to/folder/db-quarter-backups/db-$NOW.sql.gz
The result of the NOT WORKING script is a file with the expected filesize of 5MBish but no file extension. The WORKING script does the same but includes the file extension in the filename. Any assistance appreciated.
 
Old 11-07-2012, 07:34 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Best way to start debugging a shell script is to run it with debugging switches like
Code:
/bin/bash -vx /path/script --any-args 2>&1 | tee /path/debug.log
That way you see variables populated, errors fly by and you can
Code:
less "/path/debug.log"
to read back if it went too fast.
As far as basics goes I would always ensure variables are quoted properly, there's nothing else in your script that could indicate a problem:
Code:
#!/bin/bash
set -evx
NOW=$(date +"%Y%m%d")
mysqldump -h server -u user -ppassword dbname | gzip > "/path/to/folder/db-quarter-backups/db-${NOW}.sql.gz"
exit 0
*As far as standards go I would suggest against using "%b-%d-%y-%H-%M". While the output may seem valid on its own, after you've got a gazillion of files to sort and search through it suddenly no longer applies. Easier would be "%Y%m%d" and if you expect to move backups off site (which you should) then you will want something like "${HOST}-${DATE}" or "${DOMAIN}-${HOST}-${DATE}".

Code:
function howto() { echo "Bash scripting guides:
http://www.tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html 
http://www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html 
http://www.gnu.org/software/bash/manual/html_node/index.html
http://www.grymoire.com/Unix/Sh.html
http://www.tldp.org/LDP/abs/html/ 
http://mywiki.wooledge.org/BashFAQ
http://mywiki.wooledge.org/BashPitfalls"; }
 
Old 11-07-2012, 08:04 AM   #3
remedia
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thumbs down

I tried the variable with braces and that provided no joy. Is it possible there could be an issue with the file? As I am creating the file on windows? I am clutching at straws because the variable passes through the data as expected, but just leaves off the .sql.gz which is static text, really baffled.

Last edited by remedia; 11-07-2012 at 08:05 AM.
 
Old 11-07-2012, 08:43 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Saying "provided no joy" in response is not efficient and neither was leaving out the microsoft part. Show debug output instead.
 
Old 11-07-2012, 09:11 AM   #5
remedia
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
By tried and no joy I meant I had tried prior to posting on the forum and this not resolve the issue I encountered. As for the windows, the file is created on Windows 7 and then ran on Shared Hosting Linux platform(1&1), if that bares any relevance? Anything else I may have overlooked that would potentially explain this issue?

To be honest I am not knowledgeable enough with bash to attempt to debug, or to even follow you recommendations. If I find a decent tutorial to follow I will give it a go. Thanks for the help!
 
Old 11-07-2012, 09:32 AM   #6
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by remedia View Post
As for the windows, the file is created on Windows 7 and then ran on Shared Hosting Linux platform(1&1), if that bares any relevance?
Line endings mostly. If you've got a decent FOSS editor like Notepad++ you can make it save files with UNIX line endings and the use say WinSCP to copy the script over in binary mode, preserving CR/LF's. Else run 'file' on the transferred file to see which line endings it has. If it returns "ASCII text" but mentions something about line terminators (CRLF or CR, LF) then your run 'dos2unix' on it. (Also see 'man file' and 'man dos2unix'.)


Quote:
Originally Posted by remedia View Post
Anything else I may have overlooked that would potentially explain this issue?
No. Not that I can see right now.
 
Old 11-07-2012, 09:34 AM   #7
remedia
LQ Newbie
 
Registered: Nov 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Thumbs up

The dos2unix done the trick, thanks for the help much appreciated!
 
Old 11-07-2012, 09:53 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
OK, thanks for the feedback. Please marked the tread solved.
 
  


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
Make a variable = filename without the extension greengrocer Linux - Newbie 8 04-24-2011 10:16 AM
Create folder from .rar or .r00 file with filename excluding the extension (usinBash) Techno Guy Linux - Newbie 7 01-04-2009 12:13 AM
jsp: passing a variable to an included file. nkoplm Programming 4 06-15-2007 08:07 AM
How to get filename extension makowka Programming 7 04-30-2007 05:32 PM
redirect to same filename without extension geekpie Linux - Software 1 10-31-2006 10:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:33 AM.

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
Open Source Consulting | Domain Registration