从配置文件中读取分配 [英] Reading assignments from configuration files

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

问题描述

我有配置文件,其中每行包含以分号分隔的作业.像这样的东西,模仿了普通的外壳分配:

I have configuration files where each line contains assignments separated by semi-colons. Something like this, which mimics normal shell assignments :

VAR1="1"  ;  VAR2="2"
VAR1="3"  ;  VAR2="4"

每行包含相同的变量,并且打算单独处理.这些配置文件都在系统管理员的控制下,因此使用 eval 进行分配目前还不错.但是我想将此扩展到每个用户的配置文件,并且我正在寻找更好的主意.

Each line contains the same variables, and is intended to be processed individually. These configuration files are all under the system administrator control, so using eval to perform the assignment is not too bad for now. But I would like to extend this to per-user config files, and I am looking for better ideas.

我能够解析一行,并使用; 作为分隔符将其分割成块(不幸的是,该代码不允许在转义符中找到转义的; 值,但我可以接受),确定赋值(有效的变量名后跟 = 符号),然后提取赋值的正确部分(以原始格式,用引号和空格作为部分内容)价值).但是然后我有一个问题.

I am able to parse a line, split it in chunks using ; as a separator (in a way that unfortunately does not allow escaped ; to be found inside the values, but I can live with that), identify the assignment (valid variable name followed by = sign), and extract the right part of the assignment (in raw form, with quoting and spacing as part of the value). But then I have a problem.

说我有变量 value ,该变量在解析后包含像这样的手动"赋值所产生的结果:

Say I have variable value which, after the parsing, contains what would result from a "manual" assignment like this :

value="\"Arbitrary value \\\" containing escaped quote inside quotes\""

换句话说,值就是这个(如果我 echo"$ value" ):

In other words, the value is this (if I echo "$value") :

"Arbitrary value \" containing escaped quote inside quotes"

我想在不使用 eval 或其他可能导致任意代码执行(因此有代码注入风险)的方法下转换该值,从而使其变为:

I want to transform that value without using eval or another method that could cause arbitrary code execution (and therefore code injection risks) so that it becomes this:

Arbitrary value " containing escaped quote inside quotes

我想,我可以查找并删除前导和尾随的引号,但这不能处理有效的Shell引号的所有情况.如果有一种方法可以在防止代码执行的同时保留安全的扩展,那将是一个加分,但是我对此抱有很大的希望.我还希望使用仅Bash的解决方案(不调用任何外部程序),但这是优先选择,不是硬性要求.

I could, I guess, just look for and remove leading and trailing quotes, but this does not handle all cases of valid shell quoting. If there is a way to retain safe expansions while preventing code execution, that is a plus, but I am not getting my hopes up with this one. I would also prefer a Bash-only solution (no external program called), but this is a preference, not a hard requirement.

如果我解决了这个问题,我知道如何安全地执行间接分配,并且不需要有关如何读取文件,执行正则表达式匹配等的详细代码.这只是我缺少的关键步骤,我希望有一种方法不涉及编写解析器.

If I solve that issue, I know how to perform the indirect assignment safely, and I do not need detailed code on how to read files, perform regex matching, etc. It is only this critical step I am missing, and I hope there is a way that does not involve writing a parser.

推荐答案

一个非常简单的解决方案是使用 jq .由于"foo是包含引号的字符串\"是有效的json,因此它将以本机方式进行处理:

One very easy solution is to use jq. Since "foo is a string \" that contains a quote" is valid json, it handles it natively:

$ value="\"Arbitrary value \\\" containing escaped quote inside quotes\""
$ jq -r . <<< "$value"
Arbitrary value " containing escaped quote inside quotes

是的,它不是本机sh或bash,但这是一种快速简便的解决方案.此外,jq具有将结果输出回另一种shell可以读取的格式的方法:

Yes, it's not native sh or bash, but it's a quick and easy solution. Furthermore, jq has methods to output the result back to a format that can be read in by another shell:

$ jq -r '.|@sh' <<< "$value"
'Arbitrary value " containing escaped quote inside quotes'

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

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