Some Bash Trick
1 介绍 本文介绍一些我写 Bash 脚本常用的技巧,帮助我们写一些比较复杂的 Bash 脚本。说句题外话,我个人觉得用 Bash 写脚本还是比 Python 方便,当然用 Python 写更简单,门槛更低。但是 Python 太大了,很多嵌入式系统中根本不适合安装,而 Bash 的话就没有这个问题。从我的经验来说,我觉得嵌入式系统更适合用 Bash,功能也足够强大。 2 通用模块 1 命令行解析 Bash 脚本通常需要通过命令行输入参数,这时候一个好的命令行解析功能就相当重要。这里提供一个可以解析 Option 和 Command 的解析函数。 #! /usr/bin/env bash COMMAND="" # hint for users function show_usage() { cat <<EOF Usage: $0 [options] ... OPTIONS: -h, --help Display this help and exit. -a, --aoption Change it to your option. -b, --boption Change it to your option. acommand Change it to your command....