Created
April 28, 2016 11:05
-
-
Save chroju/91d8fcea846b483114362ea9daefd140 to your computer and use it in GitHub Desktop.
shell script template
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
| #!/bin/bash | |
| # | |
| # description | |
| # strict mode | |
| set -euo pipefail | |
| IFS=$'\n\t' | |
| # variables | |
| readonly COMMAND_NAME=$(basename ${0}) | |
| readonly VERSION=0.1.0 | |
| readonly RED="\e[31m" | |
| readonly GREEN="\e[32m" | |
| readonly COLOR_F="\e[m" | |
| exit_code=0 | |
| # error出力 | |
| error() { | |
| logger -isp local0.error "$@" | |
| } | |
| # usage | |
| usage() { | |
| cat <<EOF | |
| ${COMMAND_NAME} - xxx script | |
| Usage: ${COMMAND_NAME} [-ab] -p foo [-s var] [FILE] ... | |
| Options: | |
| -a .... | |
| -b .... | |
| -p .... | |
| -s .... | |
| Examples: | |
| ${COMMAND_NAME} ... | |
| EOF | |
| } | |
| # version | |
| version() { | |
| cat <<EOF | |
| ${COMMAND_NAME} v${VERSION} | |
| Copyright (C) 2016 Quants Research Inc. | |
| EOF | |
| } | |
| # logging | |
| logger -ip local0.info "${COMMAND_NAME} run by user ${USER}" | |
| # switching options | |
| while [[ $# -gt 0 ]]; do | |
| case "${1}" in | |
| --debug | -d ) | |
| set -x | |
| ;; | |
| --help | -h ) | |
| usage | |
| ;; | |
| --version | -v ) | |
| version | |
| ;; | |
| -- ) | |
| shift | |
| break | |
| ;; | |
| -* ) | |
| error "invalid option "${1}"" | |
| usage | |
| exit 1 | |
| ;; | |
| esac | |
| shift | |
| done | |
| exit ${exit_code} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment