-2

I am trying to read values from variables like this

server=$(cat "$CONF_PATH" | grep "DevServer" | cut -d'=' -f2)
echo "server is : $server "
port=$(cat "$CONF_PATH" | grep "DevPort" | cut -d'=' -f2)
echo "port is : $port "
dbname=$(cat "$CONF_PATH" | grep "DevDatabase" | cut -d'=' -f2)
echo "dbname is :   $dbname "
echo jdbc:sqlserver://$server:$port;database=$dbname;

The last echo statement is not working, I am expecting output like

jdbc:sqlserver://abc:1433;database=devdb;

where abc is server name, 1433 is port and devdb is dbname.

Stephen Kitt
  • 434,908
user86683
  • 371
  • 1
    Please provide the contents of $CONF_PATH in your question, and show us not only what you want but what the current output actually is. Don't make us guess – Chris Davies Mar 02 '22 at 23:13
  • 1
    I would guess the main problem is you're not quoting all your strings – Chris Davies Mar 02 '22 at 23:15
  • 2
    Given this is stuff with jdbc you're probably trying to parse XML, and there are much better tools for that than the ones you're using here – Chris Davies Mar 02 '22 at 23:17
  • @fuzzydrawings because there is no need to process the config files path in the script. It is the configs contents that matter – mashuptwice Mar 03 '22 at 05:33

1 Answers1

1

You simply forgot the quotes.

I've substituted the variables content, as the real input is unknown.

Without quotes:

[user@machine test]$ server=asdf
echo "server is : $server "
port=asdf2
echo "port is : $port "
dbname=asdf3
echo "dbname is :   $dbname "
echo jdbc:sqlserver://$server:$port;database=$dbname;
server is : asdf 
port is : asdf2 
dbname is :   asdf3 
jdbc:sqlserver://asdf:asdf2
[user@machine test]$ 

With the unescaped semicolon ; you are telling the shell that a new command begins. As you can see the shell is assigning $dbname to $database:

[user@machine test]$ echo $database
asdf3

With quotes:

[user@machine test]$ #!/bin/bash
server=asdf
echo "server is : $server "
port=asdf2
echo "port is : $port "
dbname=asdf3
echo "dbname is :   $dbname "
echo "jdbc:sqlserver://$server:$port;database=$dbname;"
server is : asdf 
port is : asdf2 
dbname is :   asdf3 
jdbc:sqlserver://asdf:asdf2;database=asdf3;
[user@machine test]$ 
mashuptwice
  • 1,383