如何从shell脚本修改INI文件?

发布于 2024-09-14 08:26:08 字数 256 浏览 3 评论 0原文

我正在构建 vnc 的自定义包,并希望确保在包安装后脚本中启用 GDM 的 xdcmp 设置。 gdm.conf 文件是一个 ini 样式文件,即:

[section]
var=name

我想要设置的值在整个配置文件的不同部分中存在名称冲突。

是否有任何方法或工具可以轻松地从 shell 脚本操作 ini 样式配置文件?

我想在 .deb postinst 脚本中解决这个问题。

I'm building a custom package of vnc and would like to ensure the xdcmp settings of GDM are enabled in the package post install script. The gdm.conf file is an ini style one, i.e.:

[section]
var=name

And the value I want to set has name clashes in different sections throughout the config file.

Are there any methods or tools that allow for easy manipulation of ini style config files from shell scripts?

I'd like to sort this out in the .deb postinst script.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅唱ヾ落雨殇 2024-09-21 08:26:12

使用 Ex 编辑器的 Shell 命令(更改 var 键的值):

ex +"%s/^var=\zs.*/new_name/" -scwq config.ini

要支持 INI 部分,请使用以下语法:

ex +':/\[section\]/,$s/var=\zs.*/new_name/' -scwq config.ini

要从 INI 文件读取值,请参阅:如何在 shell 脚本中获取 INI 值?


这是 shell 函数,有助于编辑 INI 值(不支持部分):

# Set value in the INI file.
# Usage: ini_set [key] [value] [file]
ini_set()
{
  local key="$1"
  local value="$2"
  local file="$3"
  [ -f "$file" ]
  if [ -n "$value" ]; then
    if grep -q "$key" "$file"; then
      echo "INFO: Setting '$key' to '$value' in $(basename "$file")"
      ex +'%s#'"$key"'=\zs.*$#'"$value"'#' -scwq! "$file"
    else
      echo "$key=$value" >> "$file"
    fi
  else
    echo "WARN: Value for '$key' is empty, ignoring."
  fi
}

这是 shell 函数读取 INI 值(不支持部分):

# Get value from the INI file.
# Usage: ini_get [key] (file)
ini_get()
{
  local key="$1"
  local file="$2"
  [ ! -s "$file" ] && return
  local value="$(grep -om1 "^$key=\S\+" "$file" | head -1 | cut -d= -f2-)"
  echo "Getting '$key' from $(basename "$file"): $value" >&2
  echo $value
}

例如 ini_get var

Shell command using Ex editor (to change the value of var key):

ex +"%s/^var=\zs.*/new_name/" -scwq config.ini

To support INI sections, use the following syntax:

ex +':/\[section\]/,$s/var=\zs.*/new_name/' -scwq config.ini

For reading values from INI files, see: How do I grab an INI value within a shell script?


Here is the shell function which can be helpful for editing INI values (not supporting sections):

# Set value in the INI file.
# Usage: ini_set [key] [value] [file]
ini_set()
{
  local key="$1"
  local value="$2"
  local file="$3"
  [ -f "$file" ]
  if [ -n "$value" ]; then
    if grep -q "$key" "$file"; then
      echo "INFO: Setting '$key' to '$value' in $(basename "$file")"
      ex +'%s#'"$key"'=\zs.*$#'"$value"'#' -scwq! "$file"
    else
      echo "$key=$value" >> "$file"
    fi
  else
    echo "WARN: Value for '$key' is empty, ignoring."
  fi
}

Here is shell function to read INI values (not supporting sections):

# Get value from the INI file.
# Usage: ini_get [key] (file)
ini_get()
{
  local key="$1"
  local file="$2"
  [ ! -s "$file" ] && return
  local value="$(grep -om1 "^$key=\S\+" "$file" | head -1 | cut -d= -f2-)"
  echo "Getting '$key' from $(basename "$file"): $value" >&2
  echo $value
}

E.g. ini_get var.

秋凉 2024-09-21 08:26:11

如果您愿意编写一些 Perl,可以使用 Config::IniFiles(包 libconfig-inifiles-perl)。

If you're willing to write some Perl, there's Config::IniFiles (package libconfig-inifiles-perl).

浅笑依然 2024-09-21 08:26:10

看看 crudini 包。它设计用于从 shell 操作 ini 文件

Have a look at the crudini package. It's designed for manipulating ini files from shell

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文