LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Variable stops file extension being included in filename (https://www.linuxquestions.org/questions/programming-9/variable-stops-file-extension-being-included-in-filename-4175436063/)

remedia 11-07-2012 06:30 AM

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.

unSpawn 11-07-2012 07:34 AM

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"; }


remedia 11-07-2012 08:04 AM

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.

unSpawn 11-07-2012 08:43 AM

Saying "provided no joy" in response is not efficient and neither was leaving out the microsoft part. Show debug output instead.

remedia 11-07-2012 09:11 AM

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!

unSpawn 11-07-2012 09:32 AM

Quote:

Originally Posted by remedia (Post 4824160)
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 (Post 4824160)
Anything else I may have overlooked that would potentially explain this issue?

No. Not that I can see right now.

remedia 11-07-2012 09:34 AM

The dos2unix done the trick, thanks for the help much appreciated!

unSpawn 11-07-2012 09:53 AM

OK, thanks for the feedback. Please marked the tread solved.


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