我为什么喜欢 Xonsh

有没有想过用 Python 做你的 shell?

Shell 语言对交互式使用很有用。但是在使用它们作为编程语言时这种优化就需要权衡,有时在编写 shell 脚本时会感觉到这点。

如果你的 shell 也能理解一种更可伸缩的语言会怎样?比如说,Python?

进入 Xonsh

安装 Xonsh 就像创建虚拟环境一样简单,运行 pip install xonsh [ptk,linux],然后运行 xonsh

首先,你可能奇怪为什么你的 Python shell 有一个奇怪的提示:

  1. <span class="pln">$ </span><span class="lit">1</span><span class="pun">+</span><span class="lit">1</span>
  2. <span class="lit">2</span>

好的,计算器!

  1. <span class="pln">$ </span><span class="kwd">print</span><span class="pun">(</span><span class="str">"hello world"</span><span class="pun">)</span>
  2. <span class="pln">hello world</span>

我们还可以调用其他函数:

  1. <span class="pln">$ </span><span class="kwd">from</span><span class="pln"> antigravity </span><span class="kwd">import</span><span class="pln"> geohash</span>
  2. <span class="pln">$ geohash</span><span class="pun">(</span><span class="lit">37.421542</span><span class="pun">,</span><span class="pun">-</span><span class="lit">122.085589</span><span class="pun">,</span><span class="pln"> b</span><span class="str">'2005-05-26-10458.68'</span><span class="pun">)</span>
  3. <span class="lit">37.857713</span><span class="pun">-</span><span class="lit">122.544543</span>

然而,我们仍然可以像常规 shell 一样使用它:

  1. <span class="pln">$ </span><span class="kwd">echo</span><span class="str">"hello world"</span>
  2. <span class="pln">hello world</span>

我们甚至可以混搭!

  1. <span class="pln">$ </span><span class="kwd">for</span><span class="pln"> i </span><span class="kwd">in</span><span class="pln"> range</span><span class="pun">(</span><span class="lit">3</span><span class="pun">):</span>
  2. <span class="pun">.</span><span class="pln">     </span><span class="kwd">echo</span><span class="str">"hello world"</span>
  3. <span class="pun">.</span>
  4. <span class="pln">hello world</span>
  5. <span class="pln">hello world</span>
  6. <span class="pln">hello world</span>

Xonsh 支持使用 Prompt Toolkit 补全 shell 命令和 Python 表达式。补全有可视化提示,会显示可能的补全并有下拉列表。

它还支持访问环境变量。它使用简单但强大的启发式方法将 Python 类型应用于环境变量。默认值为 “string”,但是,例如,路径变量是自动列表。

  1. <span class="pln">$ </span><span class="str">'/usr/bin'</span><span class="kwd">in</span><span class="pln"> $PATH</span>
  2. <span class="kwd">True</span>

Xonsh 接受 shell 形式或 Python 形式的布尔快捷运算符:

  1. <span class="pln">$ </span><span class="kwd">cat</span><span class="pln"> things</span>
  2. <span class="pln">foo</span>
  3. <span class="pln">$ </span><span class="kwd">grep</span><span class="pun">-</span><span class="pln">q foo things </span><span class="kwd">and</span><span class="kwd">echo</span><span class="str">"found"</span>
  4. <span class="pln">found</span>
  5. <span class="pln">$ </span><span class="kwd">grep</span><span class="pun">-</span><span class="pln">q bar things </span><span class="pun">&&</span><span class="kwd">echo</span><span class="str">"found"</span>
  6. <span class="pln">$ </span><span class="kwd">grep</span><span class="pun">-</span><span class="pln">q foo things </span><span class="kwd">or</span><span class="kwd">echo</span><span class="str">"found"</span>
  7. <span class="pln">$ </span><span class="kwd">grep</span><span class="pun">-</span><span class="pln">q bar things </span><span class="pun">||</span><span class="kwd">echo</span><span class="str">"found"</span>
  8. <span class="pln">found</span>

这意味着 Python 关键字是被解释了。如果我们想要打印著名的《苏斯博士》书的标题,我们需要引用关键词。

  1. <span class="pln">$ </span><span class="kwd">echo</span><span class="pln"> green eggs </span><span class="str">"and"</span><span class="pln"> ham</span>
  2. <span class="pln">green eggs </span><span class="kwd">and</span><span class="pln"> ham</span>

如果我们不这样做,我们会感到惊讶:

  1. <span class="pln">$ </span><span class="kwd">echo</span><span class="pln"> green eggs </span><span class="kwd">and</span><span class="pln"> ham</span>
  2. <span class="pln">green eggs</span>
  3. <span class="pln">xonsh</span><span class="pun">:</span><span class="typ">For</span><span class="pln"> full traceback </span><span class="kwd">set</span><span class="pun">:</span><span class="pln"> $XONSH_SHOW_TRACEBACK </span><span class="pun">=</span><span class="kwd">True</span>
  4. <span class="pln">xonsh</span><span class="pun">:</span><span class="pln"> subprocess mode</span><span class="pun">:</span><span class="pln"> command </span><span class="kwd">not</span><span class="pln"> found</span><span class="pun">:</span><span class="pln"> ham</span>
  5. <span class="typ">Did</span><span class="pln"> you mean one of the following</span><span class="pun">?</span>
  6. <span class="pln">    </span><span class="kwd">as</span><span class="pun">:</span><span class="pln">   </span><span class="typ">Command</span><span class="pun">(</span><span class="str">/usr/</span><span class="pln">bin</span><span class="pun">/</span><span class="kwd">as</span><span class="pun">)</span>
  7. <span class="pln">    ht</span><span class="pun">:</span><span class="pln">   </span><span class="typ">Command</span><span class="pun">(</span><span class="str">/usr/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">ht</span><span class="pun">)</span>
  8. <span class="pln">    mag</span><span class="pun">:</span><span class="pln">  </span><span class="typ">Command</span><span class="pun">(</span><span class="str">/usr/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">mag</span><span class="pun">)</span>
  9. <span class="pln">    ar</span><span class="pun">:</span><span class="pln">   </span><span class="typ">Command</span><span class="pun">(</span><span class="str">/usr/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">ar</span><span class="pun">)</span>
  10. <span class="pln">    </span><span class="kwd">nm</span><span class="pun">:</span><span class="pln">   </span><span class="typ">Command</span><span class="pun">(</span><span class="str">/usr/</span><span class="pln">bin</span><span class="pun">/</span><span class="kwd">nm</span><span class="pun">)</span>

虚拟环境可能会有点棘手。一般的虚拟环境(取决于它们类似 Bash 的语法)无法工作。但是,Xonsh 自带了一个名为 vox 的虚拟环境管理系统。

vox 可以创建、激活和停用 ~/.virtualenvs 中的环境。如果你用过 virtualenvwrapper,这就是环境变量所在的地方。

请注意,当前激活的环境不会影响 xonsh。它无法从激活的环境中导入任何内容。

  1. <span class="pln">$ xontrib load vox</span>
  2. <span class="pln">$ vox create </span><span class="kwd">my</span><span class="pun">-</span><span class="pln">environment                                                    </span>
  3. <span class="pun">...</span>
  4. <span class="pln">$ vox activate </span><span class="kwd">my</span><span class="pun">-</span><span class="pln">environment        </span>
  5. <span class="typ">Activated</span><span class="str">"my-environment"</span><span class="pun">.</span><span class="pln">                                                    </span>
  6. <span class="pln">$ pip install money                                                            </span>
  7. <span class="pun">...</span>
  8. <span class="pln">$ python                                                              </span>
  9. <span class="pun">...</span>
  10. <span class="pun">>>></span><span class="kwd">import</span><span class="pln"> money                                                              </span>
  11. <span class="pun">>>></span><span class="pln"> money</span><span class="pun">.</span><span class="typ">Money</span><span class="pun">(</span><span class="str">'3.14'</span><span class="pun">)</span><span class="pln">                        </span>
  12. <span class="pln">$ </span><span class="kwd">import</span><span class="pln"> money</span>
  13. <span class="pln">xonsh</span><span class="pun">:</span><span class="typ">For</span><span class="pln"> full traceback </span><span class="kwd">set</span><span class="pun">:</span><span class="pln"> $XONSH_SHOW_TRACEBACK </span><span class="pun">=</span><span class="kwd">True</span>
  14. <span class="typ">ModuleNotFoundError</span><span class="pun">:</span><span class="typ">No</span><span class="kwd">module</span><span class="pln"> named </span><span class="str">'money'</span>

第一行启用 vox:它是一个 xontrib,是 Xonsh 的一个第三方扩展。xontrib 管理器可以列出所有可能的 xontribs 及其当前状态(已安装、已加载或未加载)。

可以编写一个 xontrib 并上传到 PyPi 以使其可用。但是,最好将它添加到 xontrib 索引中,以便 Xonsh 提前知道它。比如,这能让配置向导建议它。

如果你曾经想过,“Python 可以成为我的 shell 吗?”,然后你只要 pip install xonsh 一下就能知道。


via: https://opensource.com/article/18/9/xonsh-bash-alternative

作者:Moshe Zadka 选题:lujun9972 译者:geekpi 校对:wxy

本文由 LCTT 原创编译,Linux中国 荣誉推出

相关推荐