使用shell读取ini文件方法步骤

 更新时间:2022年06月09日 09:55:59   作者:James Gosling  
本文主要介绍了使用shell读取ini文件方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

动机

我决定编写一个脚本来进行干净的 macOS (BSD) / Linux 的初始设置。 我认为有一个 tsukkomi 说“现在大多数发行版都预装了 perl / python”,但我决定使用 Shell 脚本(bash),因为它易于编写进程过程。 但是,用shell语法写各种配置文件是不可读的,所以我决定把配置文件写成.ini文件,用sed处理,然后加载到shell中。 关于使用 sed 解析 .ini 文件,如果你在 google 上搜索会出现各种示例,但我决定将其作为可以处理 .ini 文件(如)的规范,通过引用它们更具可读性。

.ini 类文件格式

  • 在 [section] 中设置节。紧接在 [和紧接之前] 之后的空白字符序列(空格 / 制表符)不被视为部分名称的一部分。 (规定可以在节名及其前后的括号之间放置空格字符。)但是,节名不应包含换行符。
  • 用 parameter = value 设置参数变量及其值。 = 前后可以有空格。假设参数名称不包括=。如果参数名称包含不能在 shell 变量中使用的字符,则在输出的 shell 变量名称中将这些字符替换为 _。
  • 如果行尾有 \,则下一行被视为续行。如果一行的开头有 4 个或更多空格/制表符,则将其视为上一行的续行。
  • 忽略 # ,; 中的行尾作为注释。
  • 忽略不包含 [and’]’ 的行,或在行首以外的位置不包含 = 的行。

在 shell 脚本中处理 .ini 文件时要执行的操作

查看部分列表。

将参数作为 shell 变量读取。 然而,作为一种变体

1. 让特定部分的参数为 shell 变量。 在某些情况下,将其设置为 shell 函数中的局部变量或环境变量。
2. 让所有或部分部分的参数为 shell 变量。 shell变量名是’基于段名的前缀’+‘参数名’。 同样在这种情况下,将其设置为局部变量或环境变量。 基于节名的前缀是通过将节名字符串中的shell变量中不能使用的字符替换为_并在末尾添加_来生成的。

假定处理系统

  • bash
  • BSD sed(我不能使用方便的 GNU sed 扩展,但我想在干净的 macOS 上运行它。)但是,我使用带有 -E 选项的扩展正则表达式。 (因为如果不能使用一个或多个匹配元字符’+',sed 语句就会变长。)

处理连续行和注释似乎有更普遍的用途,所以我单独描述。 通过将代码添加到那里描述的 sample5.sed 来实现上述内容。

查看部分列表

这相对容易。 不要处理与节名格式不匹配的行。 您所要做的就是删除与节名格式和输出相对应的行中的’[‘,’]’ 和不必要的部分。 这时候需要注意的是,换行符不是随便删的,如单独描述的,匹配换行符以外的表达式([^ \ [:space:]]] | [[ : 空白: ]]) 被使用。

:begin
$!N
s/[#;]([^[:space:]]|[[:blank:]])*([^\\[:space:]]|[[:blank:]])(\n)/\3/
s/[#;]([^[:space:]]|[[:blank:]])*(\\)(\n)/\2\3/
$s/[#;]([^[:space:]]|[[:blank:]])*$//
/(\\\n|\n[[:blank:]]{4})/ {
  s/[[:blank:]]*(\\\n|\n[[:blank:]]{4})[[:blank:]]*/ /
  t begin
}
/^[[:blank:]]*\n/ D
/\n[[:blank:]]*$/ {
  s/\n[[:blank:]]*$//
  t begin
}
/^\[([^[:space:]]|[[:blank:]])*\]/! D
s/\[[[:blank:]]*//
s/[[:blank:]]*\]([^[:space:]]|[[:blank:]])*//
P
D

例如,如果您尝试将其作为示例 .ini 文件,

# -*- mode: conf-mode ; -*- \
#;
#; sample .ini file
#;
#;

[tako]
param_a=(1 # 2 3 \
4 5 ### 6 7 
    8 9 # 10
    )

a=b # kani \
# kani \


[kani]
param_a=1
param_b=2

[uni]
param_a=3
param_b=4

[wani]
param_a=5
param_b=6

[hebi]
param_a=9
param_b=10

output example:

% sed -nE -f list_section.sed sample.ini
tako
kani
uni
wani
hebi

仅提取特定部分

下面的示例仅从上面的示例中提取特定部分 (wani) 的行,这些行遵循正确的参数定义格式。如果您找到以节名形式存在的一行,请将节名存储在保留空间中,否则如果保留空间的内容与所需的节名不匹配,则转到下一行。…如果匹配,则检查是否匹配参数格式的定义,去掉空格,本例中添加文本,使其成为shell函数的局部变量,可以总结shell变量定义。在行尾。因为已经变长了,所以加了注释行,但是作为控制结构,对每个处理都进行必要的处理,如果后面的处理没有必要,就直接回到开头. , 应该比较容易理解。

:begin
$!N
# Erase comment strings
s/[#;]([^[:space:]]|[[:blank:]])*([^\\[:space:]]|[[:blank:]])(\n)/\3/
s/[#;]([^[:space:]]|[[:blank:]])*(\\)(\n)/\2\3/
$s/[#;]([^[:space:]]|[[:blank:]])*$//
# Concatenate continuation lines
/(\\\n|\n[[:blank:]]{4})/ {
  s/[[:blank:]]*(\\\n|\n[[:blank:]]{4})[[:blank:]]*/ /
  t begin
}
# Erase blank lines
/^[[:blank:]]*\n/ D
/\n[[:blank:]]*$/ {
  s/\n[[:blank:]]*$//
  t begin
}
# Check section headline and store section name to holdspace
/^([^[:space:]]|[[:blank:]])*\[([^[:space:]]|[[:blank:]])*\]/ {
h
x
s/^([^[:space:]]|[[:blank:]])*\[(([^[:space:]]|[[:blank:]])*)\].*$/\2/g
s/^[[:blank:]]*//g
s/[[:blank:]]$//g
x
D
}
# Skip line if current section is not interested one
x
/^wani$/! { 
  x
  D
}
x
# Print if it is proper parameter definition 
/^(([^[:space:]=]|[[:blank:]])*)=(([^[:space:]]|[[:blank:]])*)/ {
  s/^[[:blank:]]*/local /
  s/[[:blank:]]*=[[:blank:]]*/=/
  s/(([^[:space:]]|[[:blank:]])*)[[:blank:]]*(\n)/\1;\3/
  P
}
D

如何限定 shell 变量名

如关于您想要做什么的部分中所述,您想要添加从部分名称生成的前缀到 shell 变量,或者如果 .ini 文件中的参数名称包含不能在 shell 变量中使用的字符串, 适当转换。 作为一种简单的方法,有多次调用sed并处理的方法,但是如果可以的话,我觉得用一个进程号sed就可以处理更漂亮。 这里最大的限制是 sed 没有一个保存空间。 在高级脚本语言中,可以将文本段划分为多个变量,进行存储、处理和组合。 另一方面,在 sed 的情况下,标准方法似乎是将多个文本作为堆栈保存和使用,并将保留空间作为分隔符并带有换行符。

例如,在下面的示例中,保留空间从第一行开始。

  • 部分名称
  • 带格式化部分名称的前缀
  • 模式空间第一行的备份
  • 模式空间第二行的备份
  • 根据.ini文件中的参数名格式化的Shell变量名

需要决定如何使用它,保持保持空间中的行数不变(下例中为 2 行),并在处理行转换时适当恢复模式空间。 由于交换保持空间和模式空间的命令只有gG和hH,类似的处理可能会重复出现,所以不可否认在1sed的过程中做起来真的很漂亮。

不管怎样,下面是从上面的 sample.ini 文件中提取 wani 和 uni 部分并输出添加了部分名称的 shell 变量的定义语句的示例。 整体控制结构保持简单,并且我添加了注释,所以我希望你能看到重写的地方以提取另一个部分,例如。

# Initialine the hold space: (Single empty line at the beginning)
1 { H ; x ; 
  # Change the expression for the defalut section name and/or variable prefix, here.
  s/^([^[:space:]]|[[:blank:]])*(\n)([^[:space:]]|[[:blank:]])*$/global\2global_/g
  x
}
:begin
$!N
# Erase comment strings
s/[#;]([^[:space:]]|[[:blank:]])*([^\\[:space:]]|[[:blank:]])(\n)/\3/
s/[#;]([^[:space:]]|[[:blank:]])*(\\)(\n)/\2\3/
$s/[#;]([^[:space:]]|[[:blank:]])*$//
# Concatenate continuation lines
/(\\\n|\n[[:blank:]]{4})/ {
  s/[[:blank:]]*(\\\n|\n[[:blank:]]{4})[[:blank:]]*/ / ; t begin
}
# Erase blank lines
/^[[:blank:]]*\n/ D
/\n[[:blank:]]*$/ {
  s/\n[[:blank:]]*$// ; t begin
}
# Check section headline and store section name to holdspace
/^([^[:space:]]|[[:blank:]])*\[([^[:space:]]|[[:blank:]])*\]/ {
  # Remove blackets and extra spaces at first line
  s/^([^[:space:]]|[[:blank:]])*\[(([^[:space:]]|[[:blank:]])*)\](([^[:space:]]|[[:blank:]])*)(\n)/\2\6/g
  s/^[[:blank:]]*//g; s/[[:blank:]]*(\n)/\1/g
  # Store the section name to the hold space, and format stored one for shell variable for the hold space
  h
  x
  s/(\n)([^[:space:]]|[[:blank:]])*$//
  s/([^[:alnum:]_]|$)/_/g
  x
  # Append the section name to the hold space.
  H
  # Remove unused line of the hold space and rearrange the remaining lines.
  x
  s/(([^[:space:]]|[[:blank:]])*)(\n)(([^[:space:]]|[[:blank:]])*)(\n)(([^[:space:]]|[[:blank:]])*)$/\4\3\1/
  x
  D
}
# Skip line if current section is not interested one
x
/^(wani|uni)(\n)/! { x ; D ; }
x
# Print if it is proper parameter definition 
/^(([^[:space:]=]|[[:blank:]])*)=(([^[:space:]]|[[:blank:]])*)/ {
  # Store current patern space text at the end of the hold space
  H

  # Build shell script variable name and store it at the end of the hold space
  s/(([^[:space:]=]|[[:blank:]])*)=.*$/\1/g
  s/^[[:blank:]]*//
  s/[[:blank:]]*$//
  s/[^[:alnum:]_]/_/g
  # If further rename of the variable name is necessary, put here.

  # Store variable name at the end of the hold space
  H

  # Build parameter variable value and keep it at pattern space
  # At first, Resore the current line text from hold space
  g
  # Remove unused lines.
  s/^(([^[:space:]]|[[:blank:]])*\n){2}//
  s/(\n([^[:space:]]|[[:blank:]])*){2}$//
  # Remove the text other than the parameter value
  s/^([^[:space:]=]|[[:blank:]])*=//g
  # If further formatting of the value is necessary, put here.

  # Append hold space stored date into pattern space
  G
  # Remove unused lines from the pattern space
  s/^(([^[:space:]]|[[:blank:]])*\n)(([^[:space:]]|[[:blank:]])*\n)(([^[:space:]]|[[:blank:]])*\n)(([^[:space:]]|[[:blank:]])*\n)(([^[:space:]]|[[:blank:]])*\n)/\1\5\9/
  # Rearrance the order of line in the pattern space, it is nessacery because only \1 ...\9 is avaiable
  s/^(([^[:space:]]|[[:blank:]])*\n)(([^[:space:]]|[[:blank:]])*\n)(([^[:space:]]|[[:blank:]])*)(\n)(([^[:space:]]|[[:blank:]])*)$/\1\3\8\7\5/

  # Format the output in the first line of the pattern space, and 
  # Restore the next line at the second line of the pattern space
  s/^(([^[:space:]]|[[:blank:]])*)(\n)(([^[:space:]]|[[:blank:]])*)(\n)(([^[:space:]]|[[:blank:]])*)(\n([^[:space:]]|[[:blank:]])*)$/local \4\7=\1;\9/

  # Clean up hold space
  x
  s/(\n([^[:space:]]|[[:blank:]])*){3}$//
  x
  P
}
D

output example:

% sed -n -E -f pickup_section2.sed sample.ini
local uni_param_a=3;
local uni_param_b=4;
local wani_param_a=5;
local wani_param_b=6;

Shell scripting

每次更改要提取的section或者切换输出格式(sh/csh、shell变量/局部变量/环境变量、变量名前缀的ON/OFF)都要重写sed文件很麻烦,所以命令行我准备了生成 sed 命令作为选项的 shell 脚本。 使用另一篇文章中的模板生成。 对于上面的seds,我还尝试了通过.ini文件的参数变量名(shell变量名)来选择输出。 (但是,如果指定了多个段名和变量名,组合不是唯一的,所以它可能不是一个很有用的功能。)

文件存储:

https://github.com/nanigashi-uji/parse_ini_sh
https://gitlab.com/nanigashi_uji/parse_ini_sh

如何使用

[Usage] % parse_ini.sh -list     file [files ...]
        % parse_ini.sh [options] file [files ...]

[Options]
    -l,--list                       : List sections 
    -S,--sec-select       name      : Section name to select
    -T,--sec-select-regex expr      : Section reg. expr. to select
    -V,--variable-select name       : variable name to select
    -W,--variable-select-regex expr : variable reg. expr. to select
    -L,--local                      : Definition as local variables (B-sh)
    -e,--env                        : Definition as enviromnental variables
    -q,--quot                       : Definition by quoting with double/single-quotation.
    -c,--csh,--tcsh                 : Output for csh statement (default: B-sh)
    -b,--bsh,--bash                 : Output for csh statement (default)
    -s,--sec-prefix                 : add prefix: 'sectionname_' to variable names. 
    -v,--verbose                    : Verbose messages 
    -h,--help                       : Show Help (this message)

output example:

% parse_ini.sh --list sample.ini
tako
kani
uni
wani
hebi

% parse_ini.sh -S kani -L sample.ini
local param_a=1;
local param_b=2;

% parse_ini.sh -S kani -L -s sample.ini
local kani_param_a=1;
local kani_param_b=2;

% parse_ini.sh -S kani -L -e -c sample.ini
setenv param_a 1;
setenv param_b 2;

% parse_ini.sh -S kani -L -e -c -q sample.ini
setenv param_a "1";
setenv param_b "2";

 到此这篇关于使用shell读取ini文件方法步骤的文章就介绍到这了,更多相关shell读取ini文件内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • linux实现解压.tar.gz文件的示例详解

    linux实现解压.tar.gz文件的示例详解

    这篇文章主要为大家详细介绍了linux实现解压.tar.gz文件的相关知识,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-08-08
  • Git 常用命令整理

    Git 常用命令整理

    本文主要介绍了Git 命令,这里对Git 常用命令进行了整理,在开发项目过程中足够用了,有需要的小伙伴可以参考下
    2016-07-07
  • linux 中vim的用法讲解

    linux 中vim的用法讲解

    Vim 是 Linux 系统上的最著名的文本/代码编辑器,也是早年的 Vi 编辑器的加强版,而 gVim 则是其 Windows 版。这篇文章主要介绍了linux 中vim的用法讲解的相关资料,需要的朋友可以参考下
    2016-10-10
  • 收集的55个Linux系统管理中常用的一些shell命令

    收集的55个Linux系统管理中常用的一些shell命令

    这篇文章主要介绍了收集的55个Linux系统管理中常用的一些shell命令,需要的朋友可以参考下
    2014-12-12
  • 解决linux下openoffice word文件转PDF中文乱码的问题

    解决linux下openoffice word文件转PDF中文乱码的问题

    下面小编就为大家带来一篇解决linux下openoffice word文件转PDF中文乱码的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-11-11
  • Shell过滤器的具体使用

    Shell过滤器的具体使用

    这篇文章主要介绍了Shell过滤器的具体使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2021-03-03
  • Linux中的内核链表实例详解

    Linux中的内核链表实例详解

    这篇文章主要介绍了Linux中的内核链表实例详解的相关资料,链表中一般都要进行初始化、插入、删除、显示、释放链表,寻找节点这几个操作,需要的朋友可以参考下
    2017-08-08
  • Linux动态库函数的详解

    Linux动态库函数的详解

    这篇文章主要介绍了Linux动态库函数的详解的相关资料,希望通过本文能帮助到大家,让大家理解掌握这部分的内容,需要的朋友可以参考下
    2017-10-10
  • shell脚本传参中包含有空格的参数

    shell脚本传参中包含有空格的参数

    本文主要介绍了shell脚本传参中包含有空格的参数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • 使用scp获取远程linux服务器上的文件 linux远程拷贝文件

    使用scp获取远程linux服务器上的文件 linux远程拷贝文件

    scp是secure copy的简写,用于在Linux下进行远程拷贝文件的命令,scp传输是加密的,下面看一下详细使用方法吧
    2014-01-01

最新评论