Date
May. 19th, 2024
 
2024年 4月 12日

Post: ShellPy: A tool for convenient shell scripting in Python

ShellPy: A tool for convenient shell scripting in Python

Published 12:03 Mar 02, 2016.

Created by @ezra. Categorized in #Programming, and tagged as #Python.

Source format: Markdown

Table of Content

Python 与 Shell 相信大家都不陌生,但你有没有想象过在 Python 中写 Shell 指令呢?如果你有兴趣,就一起来看看 ShellPy 吧!

安装 Installation

ShellPy 的安装十分简单,可以直接通过 pip install shellpy 命令从终端安装它。

使用 Introduction

除了可以使用 Shell 指令以外,ShellPy 与通常使用的 Python 没有什么不同。

在 ShellPy 中使用 Shell 指令,只需要使用一对 ` 符号将指令包围即可:

`ls -a`

当然,你也可以省略后面的部分:

`ls -l

甚至跨越多行:

`
ls -l > ls.txt
cat ls.txt
`

与 C 语言中的 Macro 类似,你也可以用类似的格式书写超长行:

`echo this is \
  a very very \
  long \
  line

每一个 ShellPy 表达式都有返回值:

result = `ls -a`

print result
print result.returncode

result_text = result.stdout
result_error = result.stderr

for line in result:
    print line.upper()

运行 Running

你可以通过 shellpy example.spy 指令来运行你的 ShellPy 文件。

Pinned Message
HOTODOGO
I'm looking for a SOFTWARE PROJECT DIRECTOR / SOFTWARE R&D DIRECTOR position in a fresh and dynamic company. I would like to gain the right experience and extend my skills while working in great teams and big projects.
Feel free to contact me.
For more information, please view online résumé or download PDF
本人正在寻求任职 软件项目经理 / 软件技术经理 岗位的机会, 希望加⼊某个新鲜⽽充满活⼒的公司。
如有意向请随时 与我联系
更多信息请 查阅在线简历下载 PDF