脚本实例
###
### my-script — does one thing well
###
### Usage:
### my-script <input> <output>
###
### Options:
### <input> Input file to read.
### <output> Output file to write. Use '-' for stdout.
### -h Show this message.
help() {
sed -rn 's/^### ?//;T;p' "$0"
}
if [[ $# == 0 ]] || [[ "$1" == "-h" ]]; then
help
exit 1
fi
sed命令说明:
sed -rn 's/^### ?//;T;p' "$0"
$0:本脚本的文件名;
-r:启用正则表达式;
n:屏蔽默认输出;
s/^### ?//:匹配### 开头的行,并删掉### ;
T:若前面替换失败则直接退出脚本的执行terminate;
p:打印替换后的结果;
测试
[root@test ~]# ./aa.sh -h
my-script — does one thing well
Usage:
my-script <input> <output>
Options:
<input> Input file to read.
<output> Output file to write. Use '-' for stdout.
-h Show this message.