阅读红宝石所有shell变量 [英] read all shell variables in ruby

查看:84
本文介绍了阅读红宝石所有shell变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行shell脚本作为Ruby脚本子,行书后,我想有一个选项来检查壳的所有环境变量,包括数组变量。

I'm running shell script as subprocess in ruby script, after running script I want to have an option to check all environment variables of the shell, including array variables.

到目前为止,我想出了:

So far I have come up with:

set | awk -F= 'BEGIN            {v=0;}
  /^[a-zA-Z_][a-zA-Z0-9_]*=/    {v=1;}
  v==1 && $2~/^['\''\$]/        {v=2;}
  v==1 && $2~/^\(/              {v=3;}
  v==2 && /'\''$/ && !/'\'\''$/ {v=1;}
  v==3 && /\)$/                 {v=1;}
  v                             {print;}
  v==1                          {v=0;}
'

这相当不错的只显示了变量,包括数组,多行字符串和筛选出的功能。

Which quite good shows only variables, including arrays, multiline strings and filtering out functions.

但是,这并不使用相同的格式,所有的时间,尤其是数组变量重新在bash和ZSH不同psented $ P $。

But this does not use the same format all the time, especially array variables are represented differently in BASH and ZSH.

这是我目前的执行: https://开头github上。 COM / mpapis / TF / BLOB /主/ lib中/ TF / environment.rb中

有一个简单的方法来显示所有将在BASH坚持工作,并且变量ZSH /可能还有其他的贝壳。

Is there an easy way to show all the variables that will work persistently in BASH and ZSH / possibly other shells.

推荐答案

很高兴再次见到你mpapis; - )

Nice to see you again mpapis ;-)

数组和关联数组并不受POSIX.1-2008 ,正如你已经发现有庆典的zsh 之间的一些恼人的细微差别。因此,有没有单一的方法在所有POSIX的炮弹做到这一点,我们需要检查 $ BASH_VERSION 等,因为你已经注意到。

Unfortunately arrays and associative arrays are not covered by POSIX.1-2008, and as you have found there are some annoying subtle differences between bash and zsh. So there is no single way to do this across all POSIX shells, and we need to check $BASH_VERSION etc. as you already noted.

我决定,这是更好地避免不必编写Ruby来解析的输出设置或其他内置插件。输出不方便解析,反正外壳知道最关心自己的数据,所以我想这是有道理的把大部分智能外壳code内。所以不是我想出了它使用的shell code输出解决方案数据结构作为 YAML 的,然后该YAML被直接加载到红宝石

I decided that it was better to avoid having to write Ruby to parse the output of set or other shell built-ins. The output is not convenient to parse, and anyway the shell knows the most about its own data, so I thought it made sense to put most of the intelligence inside the shell code. So instead I have come up with a solution which uses shell code to output the data structures as YAML, and then that YAML gets loaded directly into Ruby.

首先,我进口的参考实现和测试分为一个独立的存储库的分支。然后,我加强了测试套件,并提出了一些调整。这表明,仍然与多线处理问题。

First I imported your reference implementation and tests into the master branch of a standalone repository. Then I beefed up the test suite and made a few tweaks. This showed that there are still issues with the multi-line handling.

然后,我创建了一个新的 YAML 分支并开发了我自己的实现。我再次延长了测试。他们都通;-)请注意,我用几个不同的技巧,做反省在的zsh 庆典

Then I created a new yaml branch and developed my own implementation. Again I extended the tests. They all pass ;-) Notice that I use a few different tricks to do introspection in zsh and bash:


  • 的zsh 有一个的zsh /参数模块,它提供了包含其所有参数的名称和类型关联数组。

  • 庆典声明-p 这是使用便于解析的形式。它还具有 compgen -A变量,但最终我没有用这个。

  • zsh has a zsh/parameter module which provides associative arrays containing the names and types of all its parameters.
  • bash has declare -p which is in an easily parseable form. It also has compgen -A variable, but in the end I didn't use this.

我认为这将是很容易添加 KSH 支持吧。

I think it would be easy to add ksh support too.

这篇关于阅读红宝石所有shell变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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