【问题标题】:How to extract values from a java properties files in a bash/shell script and store in a variable and cd to that variable如何从 bash/shell 脚本中的 java 属性文件中提取值并将其存储在变量中并 cd 到该变量
【发布时间】:2013-12-04 14:05:31
【问题描述】:

我有一个 config.properties 文件,其中包含类似ouputdir=/data1/testdata/output 的路径。我能够在 shell 中提取这些并将此路径存储在变量中。当我尝试将目录更改为此路径时,我收到如下错误:No such file or directory/data1/testdata/output,尽管此路径存在。

我尝试的是:

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

通过这个,我可以打印 my_value 变量中的路径。但我无法将目录更改为 $my_value。谁能告诉我这里出了什么问题以及如何将目录更改为这个变量。

【问题讨论】:

    标签: shell properties grep cd


    【解决方案1】:

    你所拥有的应该可以工作。很明显,该目录拼写正确并且确实存在。

    对于它的价值,您可以将 grep 和 awk 命令合二为一:

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

    【讨论】:

      【解决方案2】:

      您的属性文件是否有 Windows CRLF 行尾?您的脚本可能会认为目录是 /data1/testdata/output^M 并带有尾随回车符。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2017-09-22
        • 1970-01-01
        • 1970-01-01
        • 2015-05-27
        • 2020-06-19
        • 1970-01-01
        相关资源
        最近更新 更多