从变量中读取值

从变量中读取值

我正在尝试从这样的变量中读取值

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;

最后一个 echo 语句不起作用,我期待类似的输出

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

其中abc是服务器名称,1433是端口,devdb是数据库名称。

答案1

您只是忘记了引号。

我已经替换了变量内容,因为真正的输入未知。

不带引号:

[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]$ 

使用未转义的分号;告诉 shell 一个新命令开始。正如您所看到的,shell 正在分配$dbname$database

[user@machine test]$ echo $database
asdf3

带引号:

[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]$ 

相关内容