惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
Hugging Face - Blog
Hugging Face - Blog
博客园_首页
V
V2EX
aimingoo的专栏
aimingoo的专栏
爱范儿
爱范儿
博客园 - 聂微东
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Stack Overflow Blog
Stack Overflow Blog
罗磊的独立博客
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
T
The Blog of Author Tim Ferriss
月光博客
月光博客
云风的 BLOG
云风的 BLOG

夜行人

回家路上 第一期的直播演示项目 震动检测器 正能量 在线参观CodeLab Neverland 发布 CodeLab Adapter 3.3.1 DynamicTable 之 纸糊方向盘 CodeLab DynamicTable: 一个可实施的技术方案 CodeLab Insight 发布 Alpha 版 情人节 Home Assistant 周报 && IoT 周报 (02) Joplin: 关注隐私的 Evernote 开源替代软件 浏览器的未来与 Web 传感器 Home Assistant 周报 && IoT 周报 (01) 百宝箱(01) 论自由 介绍 WebThings Home Assistant 周报 && iot 周报 (00) 百宝箱(00) 毛姆读书心得 传世之作 周末徒步 CodeLab Adapter ❤️ Jupyter/Python 航班 躲雨 夏令营途中 [译]思想--作为一种技术 The future of coding 美国之行 三门问题的程序模拟
珍爱生命,慎用shell
2016-04-10 · via 夜行人

#引子 使用linux有几个年头了,老实说,我不能想象没有shell的世界,那些精巧灵活的工具(管道/find/grep/sed/vim…)让琐事变得容易。同时我也害怕shell脚本满天飞的世界,还好大多时候我们在shell都是敲些短命令。就像害怕perl/php/javascript脚本满天飞的世界,尽管它们确实满天在飞了。。。。它们都有独到的设计,对特定问题的解决也出类拔萃,可是就语法本身而言读写起来真的不是很愉快的体验

bash的语法

相比于shell中各类工具的灵活的强大,bash语法的丑陋简直令人咋舌

这是一种典型的quick and dirty,各种临时方案风格的设计,一致性很糟糕

如果仅是使用单行命令还好,一旦试图在bash中变大逻辑,写脚本,就很痛苦了,痛苦程度要比写perl好些

带着这种既爱又恨的心情,每当看到有助于书写shell的工具,都会留意。分享一些还不错的

shellcheck

a static analysis tool for shell scripts

如果你非要用bash编程,可以试试fish,对bash的许多恶心语法做了改进,换句话说,如果你是新手,且使用fish,你需要注意它的语法和bash有不少差异,这意味着你照着书本敲的指令很可能无法运行

python

已经越来越多的系统管理员,使用python来运维系统,尤其是ansible这样的神器发布之后

对于日常的系统管理任务,python也基本都能胜任,尤其是需要写复杂逻辑的时候,bash的&{}>.[[=]]会让你发疯的,快投入python怀抱吧

我之前写的这篇文章把我日常与系统打交道用到的工具罗列了一下:Python与系统日常管理

如果你喜欢shell,又为其语法所累,我也是这样,我现在的偏好是:以python的方式使用shell,也许你也可以试试

shellpy

这是我今天早上醒来逛github看到的一个东西

日常而言,对于具体的任务,我们都习惯在shell里直接敲命令,好比git add common/ack class --python/http GET www.google.com

如果这些命令都在python用subprocess写可能会比较费事,即便使用sh这样的神器,写起来感觉还是需要看看文档,有没有一种能让我们在python更自然地书写bash的方式呢,这就是shellpy项目的初衷,它实际上是包装了subprocess

这样一来,我们能自然地使用bash中各类神器,同时使用python来控制逻辑

整个世界一下变得干净而清爽

###上手 上手很简单,看一下官方的示例就好了

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
:::text
import tempfile
import os.path
from shellpython.helpers import Dir

# We will make everything in temp directory. Dir helper allows you to change current directory
# withing 'with' block
with Dir(tempfile.gettempdir()):
    if not os.path.exists('shellpy'):
        # just executes shell command
        `git clone https://github.com/lamerman/shellpy.git

    # switch to newly created tempdirectory/shellpy
    with Dir('shellpy'):
        # here we capture result of shell execution. log here is an instance of Result class
        log = `git log --pretty=oneline --grep='Create'

        # shellpy allows you to iterate over lines in stdout with this syntactic sugar
        for line in log:
            if line.find('README.md'):
                hashcode = log.stdout.split(' ')[0]
                print hashcode
                exit(0)

        print 'The commit where the readme was created was not found'

exit(1)

执行它:shellpy example/git.spy

如果你熟悉ipython,你可能觉得这不就是把ipython的!换成`么。哈哈,我也这样觉得,不过好处在于,这样我们脱离终端,可以在python脚本里写bash啦,而不只是在ipython里

###import_from_python 我们也可以在pyhton代码中直接调用spy文件,参考:import_from_python

###更多demo example

资源

文章作者 种瓜

上次更新 2016-04-10