从 bash 脚本中的 ini 文件读取属性 [英] reading properties from ini file in a bash script

查看:42
本文介绍了从 bash 脚本中的 ini 文件读取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 shell 脚本的新手,我有一个需求,我通过使用示例来解释.我有一个属性文件,其中包含如下所示的数据:

I am new to shell scripting, and I have a need which I am explaining by using a sample example. I have a property file which will contain data as shown below :

object1 : propertyA

object2 : property1 property2

object3 : propertyxyz

注意:总的来说,我有一些对象可以具有任意数量的属性.我需要将其保存在属性文件中,以便可以根据需要进行更改.

Note : On the whole I have some objects which can have any number of properties. And I need to keep this in a property files so that it can be changed as per need.

我需要的是,如果我将对象 3 作为用户输入,那么我的代码应该打印所有相应的属性.

All I need is if I get object 3 as a user input then my code should print all the respective properties.

我曾尝试使用 .ini 文件,但我不想使用任何外部解析器.在这种情况下,bash 附带的似乎对我没有帮助.如果我使用 grep 那么我将不得不使用一些正则表达式来拆分属性.

I have tried using .ini files, but I dont want to use any external parsers. And which ever comes with bash seems to not helping me in this case. if I use grep then I would have to split the properties using some regex.

.ini 文件:

[object1]

property=propertyA


[object2]

property=property1

property=property2

[object3]

property=propertyxya

我正在寻找一种解决方案,如果我选择一个对象(在 ini 文件的情况下,它是一个部分),那么我应该获取数组中的所有属性.

I am looking for a solution in which if I select an object ( which in case of ini files it is a section) then I should get all the properties in an array.

那么是否有任何在 bash 中预定义的东西,或者我是否只需要使用 grep n regex 来编写.

So is there any thing which is predefined in bash or do I need to write using grep n regex only.

推荐答案

使用 Awk 可以很容易地漂亮完成,只需执行以下操作将内容存储在一个bash 数组.

It can be done pretty easily using Awk, just do the below to store the contents in a bash array.

read -p "Enter object to get properties for: " objectname
all_properties=($(awk -F '=' -v input="${objectname}" '$1 ~ input{flag=1; next} $1 ~ /\[object/{flag=0; next} flag && NF {split($0,arr,"="); print arr[2] }  config.ini))

现在循环数组打印属性,

Now loop the array to print the properties,

for property in "${all_properties[@]}"; do
    printf "%s\n" "$property"
done

(或)只是

printf "%s\n" "${all_properties[@]}"

Awk 命令的工作原理如下:-

The Awk command works as follows:-

  1. 在变量中获取用户输入并使用 -v 语法传递给 Awk 并执行正则表达式匹配 $1 ~ input 以匹配包含用户输入的行,比如 object1
  2. 我正在启用一个标志以从该行开始标记行并在下一个对象开始时重置标志,请参阅 $1 ~/\[object/{flag=0;下一个}
  3. 条件 flag &&NF 只负责处理请求对象之后的非空行和属性值.
  4. 现在在选定的行上,使用Awk中的split()函数,我们可以提取property的值并打印出来,稍后将其存储在数组.
  1. Getting the user input in a variable and passing to Awk using the -v syntax and doing a regex match $1 ~ input to match the line containing user input, say object1
  2. I am enabling a flag to start marking lines from this line on-wards and resetting the flag once the next object starts, see $1 ~ /\[object/{flag=0; next}
  3. The condition flag && NF takes care of processing only non-empty lines and only property values after the requested object.
  4. Now on the selected lines, using the split() function in Awk, we can extract the value of property and print it, which will be later stored in the array.

将带有 read 的行和下面的行放在 bash 脚本中,she-bang 设置为 #!/bin/bash 并运行它.

Put the line with read and the line below as shown in a bash script with she-bang set to #!/bin/bash and run it.

例如在一个完整的脚本中

E.g. in a complete script as

#!/usr/bin/bash
read -p "Enter object to get properties for: " objectname
all_properties=($(awk -F '=' -v input="${objectname}" '$1 ~ input{flag=1; next} $1 ~ /\[object/{flag=0; next} flag && NF {split($0,arr,"="); print arr[2] }' config.ini ))

printf "%s\n" "${all_properties[@]}"

在脚本上运行了一些示例.

A few sample runs on the script.

$ bash script.sh
Enter object to get properties for: object1
propertyA

$ bash script.sh
Enter object to get properties for: object2
property1
property2

$ bash script.sh
Enter object to get properties for: object3
propertyxya

这篇关于从 bash 脚本中的 ini 文件读取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆