Created
February 21, 2018 23:22
-
-
Save aks/d6fa9eff5c10e2555dd0a5c92668e075 to your computer and use it in GitHub Desktop.
A template for bash command-line utility
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # my-script some-args | |
| # set the name and directory of this program | |
| PROG="${0##*/}" | |
| DIR="${0%/*}" | |
| # include ~/bin and ~/lib to get my bash scripts | |
| export PATH=$PATH:$HOME/bin:$HOME/lib | |
| # include some library scripts | |
| source talk-utils.sh | |
| source run-utils.sh | |
| usage() { | |
| cat 1>&2 <<USAGE | |
| usage: $PROG [options] ARGUMENTS | |
| This script does this and that, and the other. You can control it with certain | |
| options. I usually explain how it works at a high-level and then explain the options. | |
| Options | |
| -h show help | |
| -i FILE read input from FILE | |
| -n norun: don't do anything, but show the commands | |
| -v be verbose; talk a lot | |
| USAGE | |
| exit | |
| } | |
| # define more bash functions here, if needed | |
| # other setup goes here | |
| # start scanning the program options | |
| norun= verbose= # ensure vars not set | |
| while getopts 'hi:nv' opt ; do | |
| case "$opt" in | |
| -h) usage ;; | |
| -n) norun=1 ;; | |
| -i) input_file="$OPTARG" ;; | |
| -v) verbose=1 ;; | |
| esac | |
| done | |
| shift $(( OPTIND - 1 )) | |
| # if no arguments, show the command usage and exit | |
| [[ $# -gt 0 ]] || usage | |
| # do any processing that depends on the options here, after parsing them | |
| exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment