nybblesAndBits
nybblesAndBits

Reputation: 273

How to concatenate a string from an included file in bash

What I'm trying to accomplish is having a central configuration file, in bash, that defines some variables that are re-used in different bash files. The example below attempts to generate a file name with the current date included in the file name as well as a variable defined in another shell script. However whenever I try to concatenate this external variable it doesn't work. I can concatenate the variable in any other situation.

Example Code:

../config/vars.sh

#!/bin/bash
mysqlUser="backupuser"
mysqlPwd="fakePwd"
mysqlSocket="/var/run/mysqld/mysqld.sock"
mysqlPort="3306"
serverName="s01.catchyservername.com"

./dbBackup.sh

#!/bin/bash
source ../config/vars.sh
tempName=$(date +"%Y%m%d.sql.gz")
fileName="mysqld_${mysqlPort}_${tempName}"
echo "mysqld_${mysqlPort}"
echo ${tempName}
echo ${fileName}

output of dbBackup.sh

mysqld_3306
20140926.sql.gz
_20140926.sql.gz

As you can see when echoing "mysqld_${mysqlPort}" I get the expected output, but when echoing ${fileName} the entire first half of the string is ignored. What am I misunderstanding?

Upvotes: 0

Views: 86

Answers (1)

Digital Trauma
Digital Trauma

Reputation: 15996

Your vars.sh file was probably created with a DOS/windows text editor:

$ ./dbBackup.sh 
mysqld_3306
20140926.sql.gz
_20140926.sql.gz
$ dos2unix vars.sh 
dos2unix: converting file vars.sh to Unix format ...
$ 
$ ./dbBackup.sh 
mysqld_3306
20140926.sql.gz
mysqld_3306_20140926.sql.gz
$ 

As you can see above, I use the dos2unix utility to convert the line separators to Unix style.

Upvotes: 2

Related Questions