Skip to content

Instantly share code, notes, and snippets.

@jecxjo
Created January 5, 2022 15:36
Show Gist options
  • Save jecxjo/7889e6c14e68d98fd481ec759f5289d7 to your computer and use it in GitHub Desktop.
Save jecxjo/7889e6c14e68d98fd481ec759f5289d7 to your computer and use it in GitHub Desktop.
Easy way to print usage using awk
#!/bin/sh
#
# This is a test script
#
# Usage: easy-shell-usage.sh [<arguments>]
#
# There aren't any arguments available, but if there were they would be documented here.
# Anything in the first block of comments starting at the "Usage" line.
# Prints usage, but this comment won't be printed
usage() {
awk '/# Usage/{ x=1 } x==1 { print $0 } !/^#/ && x==1 { exit 0 }' $0 >&2
exit 1
}
test $# -gt 0 || usage
printf "Arguments: %s\n" "$@"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment