PRF:20180904 Why I love Xonsh.md

@geekpi
This commit is contained in:
Xingyu.Wang 2018-09-14 16:59:59 +08:00
parent ba17ae4e00
commit 5b330c85ce

View File

@ -1,29 +1,34 @@
我为什么喜欢 Xonsh 我为什么喜欢 Xonsh
====== ======
> 有没有想过用 Python 做你的 shell
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/shelloff.png?itok=L8pjHXjW) ![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/shelloff.png?itok=L8pjHXjW)
Shell 语言对交互式使用很有用。但是在使用它们作为编程语言时这种优化需要权衡,有时在编写 shell 脚本时会感觉到。 Shell 语言对交互式使用很有用。但是在使用它们作为编程语言时这种优化需要权衡,有时在编写 shell 脚本时会感觉到这点
如果你的 shell 一种更可伸缩的语言会怎样比如说Python 如果你的 shell 也能理解一种更可伸缩的语言会怎样比如说Python
进入 [Xonsh][1]。 进入 [Xonsh][1]。
安装 Xonsh 就像创建虚拟环境一样简单,运行 `pip install xonsh [ptklinux]`,然后运行 `xonsh` 安装 Xonsh 就像创建虚拟环境一样简单,运行 `pip install xonsh [ptklinux]`,然后运行 `xonsh`
首先,你可能想知道为什么你的 Python shell 有一个奇怪的提示: 首先,你可能奇怪为什么你的 Python shell 有一个奇怪的提示:
``` ```
$ 1+1 $ 1+1
2 2
``` ```
好的计算器! 好的,计算器!
``` ```
$ print("hello world") $ print("hello world")
hello world hello world
``` ```
我们还可以调用其他函数: 我们还可以调用其他函数:
``` ```
$ from antigravity import geohash $ from antigravity import geohash
$ geohash(37.421542, -122.085589, b'2005-05-26-10458.68') $ geohash(37.421542, -122.085589, b'2005-05-26-10458.68')
@ -31,12 +36,14 @@ $ geohash(37.421542, -122.085589, b'2005-05-26-10458.68')
``` ```
然而,我们仍然可以像常规 shell 一样使用它: 然而,我们仍然可以像常规 shell 一样使用它:
``` ```
$ echo "hello world" $ echo "hello world"
hello world hello world
``` ```
我们甚至可以混搭! 我们甚至可以混搭!
``` ```
$ for i in range(3): $ for i in range(3):
.     echo "hello world" .     echo "hello world"
@ -49,12 +56,14 @@ hello world
Xonsh 支持使用 [Prompt Toolkit][2] 补全 shell 命令和 Python 表达式。补全有可视化提示,会显示可能的补全并有下拉列表。 Xonsh 支持使用 [Prompt Toolkit][2] 补全 shell 命令和 Python 表达式。补全有可视化提示,会显示可能的补全并有下拉列表。
它还支持访问环境变量。它使用简单但强大的启发式方法将 Python 类型应用于环境变量。默认值为 “string”但是例如路径变量是自动列表。 它还支持访问环境变量。它使用简单但强大的启发式方法将 Python 类型应用于环境变量。默认值为 “string”但是例如路径变量是自动列表。
``` ```
$ '/usr/bin' in $PATH $ '/usr/bin' in $PATH
True True
``` ```
Xonsh 接受 shell 形式或 Python 形式的布尔快捷运算符: Xonsh 接受 shell 形式或 Python 形式的布尔快捷运算符:
``` ```
$ cat things $ cat things
foo foo
@ -66,13 +75,15 @@ $ grep -q bar things || echo "found"
found found
``` ```
这意味着 Python 关键字是被解释了。如果我们想要打印著名的 Dr. Seuss 书的标题,我们需要引用关键词。 这意味着 Python 关键字是被解释了。如果我们想要打印著名的《苏斯博士》书的标题,我们需要引用关键词。
``` ```
$ echo green eggs "and" ham $ echo green eggs "and" ham
green eggs and ham green eggs and ham
``` ```
如果我们不这样做,我们会感到惊讶: 如果我们不这样做,我们会感到惊讶:
``` ```
$ echo green eggs and ham $ echo green eggs and ham
green eggs green eggs
@ -88,9 +99,10 @@ Did you mean one of the following?
虚拟环境可能会有点棘手。一般的虚拟环境(取决于它们类似 Bash 的语法无法工作。但是Xonsh 自带了一个名为 `vox` 的虚拟环境管理系统。 虚拟环境可能会有点棘手。一般的虚拟环境(取决于它们类似 Bash 的语法无法工作。但是Xonsh 自带了一个名为 `vox` 的虚拟环境管理系统。
`vox` 可以创建激活和停用 `~/.virtualenvs` 中的环境。如果你用过 `virtualenvwrapper`,这就是环境变量所在的地方。 `vox` 可以创建激活和停用 `~/.virtualenvs` 中的环境。如果你用过 `virtualenvwrapper`,这就是环境变量所在的地方。
请注意,当前激活的环境不会影响 `xonsh`。它无法从激活的环境中导入任何内容。 请注意,当前激活的环境不会影响 `xonsh`。它无法从激活的环境中导入任何内容。
``` ```
$ xontrib load vox $ xontrib load vox
$ vox create my-environment                                                     $ vox create my-environment                                                    
@ -108,7 +120,7 @@ xonsh: For full traceback set: $XONSH_SHOW_TRACEBACK = True
ModuleNotFoundError: No module named 'money' ModuleNotFoundError: No module named 'money'
``` ```
第一行启用 `vox`:它是一个 `xontrib`Xonsh 的一个第三方扩展。`xontrib` 管理器可以列出所有可能的 `xontribs` 及其当前状态(已安装已加载或未加载)。 第一行启用 `vox`:它是一个 `xontrib`Xonsh 的一个第三方扩展。`xontrib` 管理器可以列出所有可能的 `xontribs` 及其当前状态(已安装已加载或未加载)。
可以编写一个 `xontrib` 并上传到 `PyPi` 以使其可用。但是,最好将它添加到 `xontrib` 索引中,以便 Xonsh 提前知道它。比如,这能让配置向导建议它。 可以编写一个 `xontrib` 并上传到 `PyPi` 以使其可用。但是,最好将它添加到 `xontrib` 索引中,以便 Xonsh 提前知道它。比如,这能让配置向导建议它。
@ -121,7 +133,7 @@ via: https://opensource.com/article/18/9/xonsh-bash-alternative
作者:[Moshe Zadka][a] 作者:[Moshe Zadka][a]
选题:[lujun9972](https://github.com/lujun9972) 选题:[lujun9972](https://github.com/lujun9972)
译者:[geekpi](https://github.com/geekpi) 译者:[geekpi](https://github.com/geekpi)
校对:[校对者ID](https://github.com/校对者ID) 校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出