「Shell」- 常用代码

explainshell.com

解释 Shell 命令的网站;
该站点的源码在 GitHub 中:https://github.com/idank/explainshell

将 Unicode 与 Ascii 互相转化

使用 ascii2uni 和 uni2ascii 在 Unicode 和 Ascii 之间互转;

使用 printf 也可以做到;

执行生成的命令

scripting – Bash: Execute piped lines from stdin – Server Fault

我们会通过 Shell 脚本来生成一些命令,然后再执行这些命令:python example.py | bash

或,更具数据直接组装并执行命令,但有时候这种做法更烦琐;

输入特殊字符

比如输入一个 TAB 键,但是 Bash 中 TAB 用于补全提示。。;

先按下 Ctrl+V,然后在按下 TAB 键;

当 BASH 无响应时

使用 Ctrl+J 组合键;或者输入 reset 内建命令;

修改运行进程的输出

redirecting output of running background job in bash
How to redirect output of an already running process

脚本优化

Bash script; optimization of processing speed

“The first rule of optimization is: don’t optimize.”

如果数据量大的话还是要换语言(当然,这要你自己控制成本、时间、性能上的平衡)。

倒是有些通用的小办法,可以参考「Bash script; optimization of processing speed」一文中的说明。

你可以自己优化程序,但是你要防止“在优化后,逻辑不如原来清晰、直接”的问题。

参考文献

Read the json data in shell script
How to convert \uXXXX unicode to UTF-8 using console tools in *nix