How to extract values from a java properties files in a bash/shell script and store in a variable and cd to that variable

∥☆過路亽.° 提交于 2019-12-01 06:47:29

问题


I have a config.properties file which contains a path like ouputdir=/data1/testdata/output. I am able to extract these in shell and store this path in a variable. While I am trying to change directory to this path, I am getting error something like: No such file or directory/data1/testdata/output, thouth this path exists.

What I tried is:

configPath=/data1/testdata.config.properties
my_value=$(grep outputdir $configPath|  awk -F= '{print $2}')
echo $my_value
cd $my_value

by this I am able to print the path in my_value variable.but I am not able to change directory to $my_value.can anybody tell me what is wrong here and how can I change directory to this variable.


回答1:


What you have should work. Check the obvious, that the directory is spelled right and does exist.

For what it's worth, you could combine the grep and awk commands into one:

my_value=$(awk -F= '$1=="outputdir" {print $2}')



回答2:


Does your properties file have windows CRLF line endings? your script may think the directory is /data1/testdata/output^M with the trailing carriage return.

sed -n '/^outputdir=/{s///; s/\r$//; p; q}' properties.file


来源:https://stackoverflow.com/questions/20377481/how-to-extract-values-from-a-java-properties-files-in-a-bash-shell-script-and-st

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!