【问题标题】:Need bash shell script for reading name value pairs from a file需要 bash shell 脚本来从文件中读取名称值对
【发布时间】:2011-06-26 19:25:40
【问题描述】:

我有一个类似的文件

name1=value1
name2=value2

我需要使用 shell 脚本读取这个文件并设置变量

$name1=value1
$name2=value2

请提供可以执行此操作的脚本。

我尝试了下面的第一个答案,即获取属性文件,但如果值包含空格,我会遇到问题。它被解释为空格后的新命令。我怎样才能让它在存在空间的情况下工作?

【问题讨论】:

标签: shell properties-file


【解决方案1】:

如果输入文件中的所有行都是这种格式,那么只需对其进行采购就会设置变量:

source nameOfFileWithKeyValuePairs

. nameOfFileWithKeyValuePairs

【讨论】:

  • 嘿,如果值包含空格,我会遇到问题。它被解释为空格后的新命令。请告诉如何让它在存在空间的情况下工作。
  • 我的钥匙上有圆点(例如data.dir=/path/to/data/dir)。它会根据值给出诸如Command not foundNo such file or directory 之类的错误。知道如何解决这个问题吗?
  • 正如此处其他地方所指出的,'source' 适用于许多情况,但 a) 可能具有安全隐患,并且 b) 不适用于其中包含例如“$”的值。
  • 为什么你不应该源文件(大的安全风险) - unix.stackexchange.com/a/433245/364638
【解决方案2】:

用途:

while read -r line; do declare  "$line"; done <file

【讨论】:

  • 好的,我在阅读 -r 行时认为这是可行的;声明“$line”; done
  • @kurumi,请考虑编辑此内容以添加引号。没有它们,它错误的。
【解决方案3】:

使用.source 获取文件的问题是您也可以将执行的命令放入其中。如果输入不是绝对可信的,那就有问题了(你好rm -rf /)。

如果已知的键数量有限,您可以使用read 读取这样的键值对:

read_properties()
{
  file="$1"
  while IFS="=" read -r key value; do
    case "$key" in
      "name1") name1="$value" ;;
      "name2") name2="$value" ;;
    esac
  done < "$file"
}

【讨论】:

  • 这很棒,因为它可以匹配带有点的键:"key.subkey") name1="$value" ;;
  • 请注意,您的属性文件的最后一行需要一个尾随新行 \n 字符,否则将无法读取。
【解决方案4】:

如果您的文件位置是/location/to/file 并且密钥是mykey

grep mykey $"/location/to/file" | awk -F= '{print $2}'

【讨论】:

    【解决方案5】:

    假设你的文件名是some.properties

    #!/bin/sh
    # Sample shell script to read and act on properties
    
    # source the properties:
    . some.properties
    
    # Then reference then:
    echo "name1 is $name1 and name2 is $name2"
    

    【讨论】:

    • 虽然是这样,但它应该带有一个闪烁的红色大警告标签,上面写着“EVAL!EVAL!你在这里使用 EVAL!”除非对输入文件有绝对的控制权。
    • 如果我以 .some.properties 和 . ./some.properties
    【解决方案6】:

    @robinst 的改进版

    read_properties()
    {
      file="$1"
      while IFS="=" read -r key value; do
        case "$key" in
          '#'*) ;;
          *)
            eval "$key=\"$value\""
        esac
      done < "$file"
    }
    

    变化:

    • 动态键映射而不是静态键映射
    • 支持(跳过)注释行

    一个不错的也是@kurumi的解决方案,但是busybox不支持

    这里有一个完全不同的变体:

    eval "`sed -r -e "s/'/'\\"'\\"'/g" -e "s/^(.+)=(.+)\$/\1='\2'/" $filename`"
    

    (我试图在转义方面做得最好,但我不确定这是否足够)

    【讨论】:

    • 这个不错,就是没有读到最后一行。如何解决?
    • @Andrew 好点,不知道这个。似乎是read -r key value 中的某些东西,如果没有循环,我什至无法重现。有什么帮助吗?
    【解决方案7】:

    使用 shdotenv

    dotenv 支持 shell 和 POSIX 兼容的 .env 语法规范 https://github.com/ko1nksm/shdotenv

    Usage: shdotenv [OPTION]... [--] [COMMAND [ARG]...]
    
      -d, --dialect DIALECT  Specify the .env dialect [default: posix]
                               (posix, ruby, node, python, php, go, rust, docker)
      -s, --shell SHELL      Output in the specified shell format [default: posix]
                               (posix, fish)
      -e, --env ENV_PATH     Location of the .env file [default: .env]
                               Multiple -e options are allowed
      -o, --overload         Overload predefined environment variables
      -n, --noexport         Do not export keys without export prefix
      -g, --grep PATTERN     Output only those that match the regexp pattern
      -k, --keyonly          Output only variable names
      -q, --quiet            Suppress all output
      -v, --version          Show the version and exit
      -h, --help             Show this message and exit
    

    将 .env 文件加载到您的 shell 脚本中。

    eval "$(shdotenv [OPTION]...)"
    

    【讨论】:

      【解决方案8】:

      sed 's/^/\$/' yourfilename

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-02-10
        • 1970-01-01
        • 2017-09-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多