Shell 脚本如何输出帮助信息?

作者展示了一个技巧,将帮助信息写在 Bash 脚本脚本的头部,然后只要执行"脚本名 + help",就能输出这段帮助信息
https://samizdat.dev/help-message-for-shell-scripts/

#!/bin/bash
###
### 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() {
	awk -F'### ' '/^###/ { print $2 }' "$0"
}

if [[ $# == 0 ]] || [[ "$1" == "-h" ]]; then
	help
	exit 1
fi


echo Hello World

posted on 2020-07-17 17:45  明天有风吹  阅读(2315)  评论(0编辑  收藏  举报

导航

+V atob('d2h5X251bGw=')

请备注:from博客园