Linux 如何从Shell脚本的配置文件中检索值(不知道键)

Linux 如何从Shell脚本的配置文件中检索值(不知道键),linux,bash,shell,Linux,Bash,Shell,根据我的场景,需要从配置文件中收集值。但需要访问配置文件中的值,而无需指定密钥 通过source命令,我通过以下方式检索值 Configuration.conf 导出名称=值 出口年限=价值 导出地址=值 Script.sh 通过上述方法,我可以访问配置文件中的值 我希望访问这些值而不使用配置文件的键 在我上面的场景中,键将以任何形式发生变化,但值将和我的逻辑相似。在脚本中,我必须在不知道键名的情况下读取值。类似下面的内容 source Configuration.conf while [ $1

根据我的场景,需要从配置文件中收集值。但需要访问配置文件中的值,而无需指定密钥

通过source命令,我通过以下方式检索值

Configuration.conf

导出名称=值

出口年限=价值

导出地址=值

Script.sh

通过上述方法,我可以访问配置文件中的值

我希望访问这些值而不使用配置文件的键

在我上面的场景中,键将以任何形式发生变化,但值将和我的逻辑相似。在脚本中,我必须在不知道键名的情况下读取值。类似下面的内容

source Configuration.conf
while [ $1 ] // Here 1 is representing the first Key of the configuration file. 
do
//My Logics
done

非常感谢您的帮助。

我将使用sed和cut解析配置文件。像这样:

sed -n 's/export//p' conf.sh | while read expression ; do 
    key=$(cut -d= -f1 <<< "$expression")
    value=$(cut -d= -f2 <<< "$expression")

    # your logic comes here ...
    echo "$key -> $value"
done

我将使用sed和cut解析配置文件。像这样:

sed -n 's/export//p' conf.sh | while read expression ; do 
    key=$(cut -d= -f1 <<< "$expression")
    value=$(cut -d= -f2 <<< "$expression")

    # your logic comes here ...
    echo "$key -> $value"
done

假设配置文件只包含var=value声明,每个声明占用一行

configfile=./Configuration.conf

. "$configfile"

declare -A configlist

while IFS='=' read -r key val ; do

  # skip empty / commented lines and obviously invalid input
  [[ $key =~ ^[[:space:]]*[_[:alpha:]] ]] || continue

  # Stripping up to the last space in $key removes "export".
  # Using eval to approximate the shell's handling of lines like this:
  #    var="some thing with spaces" # and a trailing comment.
  eval "configlist[${key##* }]=$val"

done < "$configfile"

# The keys are "${!configlist[@]}", the values are "${configlist[@]}"
#
#for key in "${!configlist[@]}" ; do
#  printf '"%s" = "%s"\n' "$key" "${configlist[$key]}"
#done

for value in "${configlist[@]}" ; do
  : your logic goes here
done

假设配置文件只包含var=value声明,每个声明占用一行

configfile=./Configuration.conf

. "$configfile"

declare -A configlist

while IFS='=' read -r key val ; do

  # skip empty / commented lines and obviously invalid input
  [[ $key =~ ^[[:space:]]*[_[:alpha:]] ]] || continue

  # Stripping up to the last space in $key removes "export".
  # Using eval to approximate the shell's handling of lines like this:
  #    var="some thing with spaces" # and a trailing comment.
  eval "configlist[${key##* }]=$val"

done < "$configfile"

# The keys are "${!configlist[@]}", the values are "${configlist[@]}"
#
#for key in "${!configlist[@]}" ; do
#  printf '"%s" = "%s"\n' "$key" "${configlist[$key]}"
#done

for value in "${configlist[@]}" ; do
  : your logic goes here
done

使用grep从conf文件中提取密钥。使用获取值


使用grep从conf文件中提取密钥。使用获取值


您给出的示例,[$1]将执行相同的操作,无论参数是什么。不清楚你们想要达到什么目的。@devnull:真的很抱歉把问题提得很混乱。现在我已经改变了。希望它能对您有所帮助。您给出的示例,[$1]也会这样做,无论参数是什么。不清楚你们想要达到什么目的。@devnull:真的很抱歉把问题提得很混乱。现在我已经改变了。希望它能帮助你。它工作得很好。非常感谢你。对你的代码有疑问。请与我分享你的知识。如果不使用源代码命令,您的代码工作正常。这怎么可能?您在内存中存储密钥和值的方式您必须事先在当前shell中获取conf文件的来源。启动一个新的shell并尝试againIt,它工作正常。非常感谢你。对你的代码有疑问。请与我分享你的知识。如果不使用源代码命令,您的代码工作正常。这怎么可能?您在内存中存储密钥和值的方式您必须事先在当前shell中获取conf文件的来源。启动新shell并重试
0   Name
1   Age
2   Address

Name    => name_value
Age     => age_value
Address => addr_value