Linux 从 shell 脚本中的属性文件读取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18711700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:47:52  来源:igfitidea点击:

Reading data from properties file in shell script

linuxshellproperties-file

提问by charan

I am writing a shell script which reads data from a properties file and stores in into a local variable in shell script. The problem is when i am trying to read multiple properties from the file and form a string its getting over written

我正在编写一个 shell 脚本,它从属性文件中读取数据并将其存储到 shell 脚本中的局部变量中。问题是当我试图从文件中读取多个属性并形成一个字符串时,它会被覆盖

#!/bin/bash
. /opt/oracle/scripts/user.properties

echo $username
echo $password
echo $service_name

conn=$username$password$service_name
echo $conn

the values of username=xxxx password=yyyy and service_name=zzzz i expect the output to be

username=xxxx password=yyyy 和 service_name=zzzz 的值我希望输出是

xxxxyyyyzzzz 

but instead of that i am getting the output as

但不是这样,我得到的输出是

zzzz

please tell me where am i doing the mistake ?

请告诉我我在哪里做错了?

采纳答案by devnull

I'm certain that the file /opt/oracle/scripts/user.propertiescontains CR+LFline endings. (Running the filecommand for the properties file would say ... with CRLF line terminators). Changing those to LFusing dos2unixor any other utility should make it work.

我确定该文件/opt/oracle/scripts/user.properties包含CR+LF行结尾。(运行file属性文件的命令会说... with CRLF line terminators)。将它们更改为LFusingdos2unix或任何其他实用程序应该可以使其工作。

Moreover, instead of saying:

此外,而不是说:

conn=$username$password$service_name

you could say:

你可以说:

conn="${username}${password}${service_name}"