0

I have a configuration file like this:

//filename : stat.conf
LAS_SERVER="127.0.0.1"
LAS_PORT=3306
LAS_USER=root
LAS_PWD=root
LAS_DB=test
CONN_STR_LAS_DB="-h$LAS_SERVER -P$LAS_PORT -u$LAS_USER -p$LAS_PWD $LAS_DB"

Now I run the source command like this:

$ source ./stat.conf

$ echo $CONN_STR_LAS_DB
testot.0.1

You can see that the result is wrong. not in our expectation.

In contrast, I can get the right result if I run the command in the shell like this:

$ LAS_SERVER="127.0.0.1"
$ LAS_PORT=3306
$ LAS_USER=root
$ LAS_PWD=root
$ LAS_DB=test
$ CONN_STR_LAS_DB="-h$LAS_SERVER -P$LAS_PORT -u$LAS_USER -p$LAS_PWD $LAS_DB"
$ echo $CONN_STR_LAS_DB
-h127.0.0.1 -P3306 -uroot -proot test

This is the right result.

So, my question is: why I got the wrong result when using "source ./stat.conf" ?

I tested the same operation on another computer, I can get the right result. Is there anything that I missed to config on my computer?

My OS is CentOS 5.

2 Answers 2

6

Your config file has windows-style line endings (\r\n), not unix-style (just \n). You can use the dos2unix command to convert it. Then, switch to a text editor that doesn't create files with weird line endings.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much ! That's the solution !
@user1284984: If this solution has worked for you, you should accept it by clicking the tick to the left of this answer.
-2

I know what's wrong with your shell code.

Thatis the results of environment variables,your $CONN_STR_LAS_DB declared in your shell script can be exist just within the run-time of this script,and of course you can not echo $CONN_STR_LAS_DB outside this scripts.

The way to solve this problem is to "export" your variables ,that is

export $CONN_STR_LAS_DB                

in your shell script,and that surely can be done! good luck!

2 Comments

Sorry, not the case. When you run a script it can't affect its parent's environment variables, but sourcing (as is done here) runs the commands in the current environment, so it can (this is why .bashrc and other configuration files work). The problem here is windows line endings, see Gordon's answer.
The whole point of source (or the . dot command) is that the file is read and its contents executed in the current shell, precisely so that it can change the shell's environment (because it is not run in a child process).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.