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

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
宝玉的分享
宝玉的分享
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Darknet – Hacking Tools, Hacker News & Cyber Security
V
Vulnerabilities – Threatpost
博客园_首页
Engineering at Meta
Engineering at Meta
F
Fortinet All Blogs
C
Cyber Attacks, Cyber Crime and Cyber Security
罗磊的独立博客
V
Visual Studio Blog
Know Your Adversary
Know Your Adversary
Hacker News - Newest:
Hacker News - Newest: "LLM"
美团技术团队
L
LINUX DO - 最新话题
The Last Watchdog
The Last Watchdog
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
云风的 BLOG
云风的 BLOG
N
Netflix TechBlog - Medium
MyScale Blog
MyScale Blog
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
I
InfoQ
Last Week in AI
Last Week in AI
V2EX - 技术
V2EX - 技术
量子位
S
Secure Thoughts
L
LangChain Blog
The Hacker News
The Hacker News
H
Help Net Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
K
Kaspersky official blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
I
Intezer
Vercel News
Vercel News
Hacker News: Ask HN
Hacker News: Ask HN
Cisco Talos Blog
Cisco Talos Blog
Google DeepMind News
Google DeepMind News
S
Securelist
阮一峰的网络日志
阮一峰的网络日志
G
Google Developers Blog
Help Net Security
Help Net Security
Martin Fowler
Martin Fowler
爱范儿
爱范儿
Y
Y Combinator Blog
C
Check Point 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 美国之行 三门问题的程序模拟 从Python转向Pharo https://blog.just4fun.site/post/iot/iot-open-source-projects/ Python异步编程笔记 https://blog.just4fun.site/post/iot/iot-open-source-hardware-community/ 万物积木化开发者社区 CodeLab ❤️ Blender Scratch3技术分析之云变量 API(第7篇) [译]对管道(Pipes)的偏爱 [译]提出正确的问题比得到正确答案更重要 蓝牙设备与Scratch3.0 创建你的第一个Scratch3.0 Extension Scratch3技术分析之项目内部数据(第6篇) Scratch3技术分析之社区 API(第5篇) Scratch3技术分析之User API(第4篇) Scratch3技术分析之项目主页API(第3篇) Scratch3技术分析之静态资源API(第2篇) Scratch3.0、micro:bit与Windows7 https://blog.just4fun.site/post/iot/zerynth-vs-micropython/ 核聚变、方所与半宅空间 可视化编程为何是个糟糕的主意 codelab.club周末聚会 关于codelab.club '下一件大事'是一个房间 Hungry Robot - Eat everything 编程作为一种思考方式 今日简史 史蒂夫·乔布斯传 罗素自选文集 https://blog.just4fun.site/post/edx/tianjin-scratch-ai/ https://blog.just4fun.site/post/edx/richie-cms-openedx/ 徒步武功山 WebUSB与micro:bit 积木化编程与3D场景 夜宿武功山顶 scratch3-adapter接入优必选Alpha系列机器人 https://blog.just4fun.site/post/edx/video-migration-note/ scratch3-adapter重构笔记 https://blog.just4fun.site/post/edx/edx-community-members/ 两种硬件编程风格的比较 使用micro:bit自制PPT翻页笔 柏拉图对话集 scratch3.0 + micro:bit 七月电影放映计划 非营利组织的管理 Screenly--用树莓派让任何屏幕变为可编程的数字标牌 以最佳实践开始你的Django项目 micro:bit与事件驱动 为Scratch3.0设计的插件系统(上篇) OCR应用一例 近两年读过的一些好书 blockly开发之使用python驱动浏览器中的turtle(2) 牛顿新传 文学理论入门 逻辑的引擎 人生的意义 blockly开发之生成并运行js代码(1) blockly开发之hello world(0) micro:bit使用笔记 神器之Termux https://blog.just4fun.site/post/iot/micropython-notes/ Cozmo what is this Scratch的前世今生 下段旅程 我行在远方 爆裂 途中杂记 https://blog.just4fun.site/post/edx/open-edx-startup/ cozmo系列之入门 - 有性格且可编程的机器人 PaperWeekly开发笔记 创业二三事
珍爱生命,慎用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