如何从 bash/shell 脚本中的 java 属性文件中提取值并将其存储在变量中并 cd 到该变量 [英] How to extract values from a java properties files in a bash/shell script and store in a variable and cd to that variable
问题描述
我有一个 config.properties 文件,其中包含类似 ouputdir=/data1/testdata/output
的路径.我能够在 shell 中提取这些并将此路径存储在一个变量中.当我尝试将目录更改为此路径时,出现类似以下错误:No such file or directory/data1/testdata/output
, thoutth this path exists.
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.
我尝试的是:
configPath=/data1/testdata.config.properties
my_value=$(grep outputdir $configPath| awk -F= '{print $2}')
echo $my_value
cd $my_value
由此我可以打印 my_value 变量中的路径.但我无法将目录更改为 $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.
推荐答案
你所拥有的应该有效.检查显而易见的,该目录拼写正确并且确实存在.
What you have should work. Check the obvious, that the directory is spelled right and does exist.
就其价值而言,您可以将 grep 和 awk 命令合二为一:
For what it's worth, you could combine the grep and awk commands into one:
my_value=$(awk -F= '$1=="outputdir" {print $2}')
这篇关于如何从 bash/shell 脚本中的 java 属性文件中提取值并将其存储在变量中并 cd 到该变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!