Success to load the configuration from config file, but failed when connect those values to sqlurl string
<p>In a project, I use shell script to access and operation MySQL.</p>

<p>At first, I write the ip, port, username, password to connect mysql in shell script itself, and everything is ok! Then, I want to config the ip, port, username, password in config file, but it's failed. In fact, it's success to load the ip, port, username, password from config file, but failed when connect those values to sqlurl string.</p>

<p>=========================================================</p>

<h1>First method.</h1>

<p>[Code]</p>

<pre><code>    IPADDRESS="127.0.0.1"
PORT=3306
USER="abc"
PASSWORD="abc123"
echo "IPADDRESS: $IPADDRESS"
echo "PORT: $PORT"
echo "USER: $USER"
echo "PASSWORD: $PASSWORD"
echo ""

SQLURL="-h$IPADDRESS -P$PORT -u$USER -p$PASSWORD"
echo "SQLURL = $SQLURL"
echo ""
</code></pre>

<p>[Output]</p>

<pre><code>IPADDRESS: 127.0.0.1
PORT: 3306
USER: abc
PASSWORD: abc123

SQLURL = -h127.0.0.1 -P3306 -uabc -pabc123
</code></pre>

<p>=========================================================</p>

<h1>Second method.</h1>

<p>[configration file]
configuration filename is config.ini, and the content is:</p>

<pre><code>IPADDRESS=127.0.0.1
    PORT=3306
    USER=abc
    PASSWORD=abc123
</code></pre>

<p>[code]</p>

<pre><code># method 2.1 use the function I wrote myself
# get_config_value_by_key $filename $key
function get_config_value_by_key() {
    __filename=$1
    __key=$2

    while read line; do
        key=`echo $line|awk -F '=' '{print $1}'`
        value=`echo $line|awk -F '=' '{print $2}'`

        if [ $key == $__key ]; then
            echo $value
        fi
    done &lt; $__filename
}

configfile="config.ini"
echo "configfile = $configfile"
echo ""

IPADDRESS="`get_config_value_by_key $configfile "IPADDRESS"`"
PORT=`get_config_value_by_key $configfile "PORT"`
USER="`get_config_value_by_key $configfile "USER"`"
PASSWORD="`get_config_value_by_key $configfile "PASSWORD"`"
echo "IPADDRESS: $IPADDRESS"
echo "PORT: $PORT"
echo "USER: $USER"
echo "PASSWORD: $PASSWORD"
echo ""

SQLURL="-h$IPADDRESS -P$PORT -u$USER -p$PASSWORD"
echo "SQLURL = $SQLURL"
echo ""

# method 2.2 use sed
IPADDRESS=`sed -n -e 's/^\s*IPADDRESS\s*=\s*//p' $configfile`
PORT=`sed -n -e 's/^\s*PORT\s*=\s*//p' $configfile`
USER=`sed -n -e 's/^\s*USER\s*=\s*//p' $configfile`
PASSWORD=`sed -n -e 's/^\s*PASSWORD\s*=\s*//p' $configfile`
echo "IPADDRESS: $IPADDRESS"
echo "PORT: $PORT"
echo "USER: $USER"
echo "PASSWORD: $PASSWORD"
echo ""

SQLURL="-h$IPADDRESS -P$PORT -u$USER -p$PASSWORD"
echo "SQLURL = $SQLURL"
echo ""
</code></pre>

<p>[Output]</p>

<pre><code>configfile = config.ini

IPADDRESS: 127.0.0.1
PORT: 3306
USER: abc
PASSWORD: abc123

 -pabc123-h127.0.0.1

IPADDRESS: 127.0.0.1
PORT: 3306
USER: abc
PASSWORD: abc123

 -pabc123-h127.0.0.1
</code></pre>

<p>=====================================================</p>

<h1>Compare</h1>

<p>the correct sqlurl output is "SQLURL = -h127.0.0.1 -P3306 -uabc -pabc123"
the wrong sqlurl output is "-pabc123-h127.0.0.1" or otherthings, lost most contents.</p>