-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tom Scavo
committed
Apr 20, 2017
1 parent
815b33d
commit 75fe9ed
Showing
4 changed files
with
217 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,208 @@ | ||
| #!/bin/bash | ||
|
|
||
| ####################################################################### | ||
| # Copyright 2013--2017 Internet2 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| ####################################################################### | ||
|
|
||
| #export COMMAND_PATHS=true | ||
| _COMPATIBILITY_MODE=true | ||
|
|
||
| ####################################################################### | ||
| # Specify the absolute path to each shell command file. | ||
| # | ||
| # For example, instead of writing code such as: | ||
| # | ||
| # cat $file | grep $pattern | ||
| # | ||
| # we write this instead: | ||
| # | ||
| # $_CAT $file | $_GREP $pattern | ||
| # | ||
| # Scripts written in this way do not depend on the underlying PATH | ||
| # and therefore function well across platforms and as cron jobs. | ||
| # | ||
| # This script is compatible with Mac OS and GNU/Linux. | ||
| ####################################################################### | ||
|
|
||
| # commands with compatible paths | ||
| _BASE64=/usr/bin/base64 | ||
| _BC=/usr/bin/bc | ||
| _CAT=/bin/cat | ||
| _CMP=/usr/bin/cmp | ||
| _CP=/bin/cp | ||
| _CURL=/usr/bin/curl | ||
| _DATE=/bin/date | ||
| _DD=/bin/dd | ||
| _DIFF=/usr/bin/diff | ||
| _DIRNAME=/usr/bin/dirname | ||
| _ECHO=/bin/echo # also a bash builtin | ||
| _FIND=/usr/bin/find | ||
| _HEAD=/usr/bin/head | ||
| _LS=/bin/ls | ||
| _MKDIR=/bin/mkdir | ||
| _MV=/bin/mv | ||
| _OPENSSL=/usr/bin/openssl | ||
| _PRINTF=/usr/bin/printf # also a bash builtin | ||
| _RM=/bin/rm | ||
| _RMDIR=/bin/rmdir | ||
| _TAIL=/usr/bin/tail | ||
| _TEE=/usr/bin/tee | ||
| _TR=/usr/bin/tr | ||
| _UNIQ=/usr/bin/uniq | ||
| _WC=/usr/bin/wc | ||
| _XARGS=/usr/bin/xargs | ||
| _XSLTPROC=/usr/bin/xsltproc | ||
|
|
||
| # commands with incompatible paths | ||
| if [[ ${OSTYPE} = darwin* ]] ; then | ||
|
|
||
| _AWK=/usr/bin/awk | ||
| _BASENAME=/usr/bin/basename | ||
| _CUT=/usr/bin/cut | ||
| _GREP=/usr/bin/grep | ||
| _GZIP=/usr/bin/gzip | ||
| _GUNZIP=/usr/bin/gunzip | ||
| _MKTEMP=/usr/bin/mktemp | ||
| _MORE=/usr/bin/more | ||
| _SED=/usr/bin/sed | ||
| _SEDEXT="/usr/bin/sed -E" | ||
| _SORT=/usr/bin/sort | ||
| _TOUCH=/usr/bin/touch | ||
|
|
||
| elif [[ ${OSTYPE} = linux* ]] ; then | ||
|
|
||
| _AWK=/bin/awk | ||
| _BASENAME=/bin/basename | ||
| _CUT=/bin/cut | ||
| _GREP=/bin/grep | ||
| _GZIP=/bin/gzip | ||
| _GUNZIP=/bin/gunzip | ||
| _MKTEMP=/bin/mktemp | ||
| _MORE=/bin/more | ||
| _SED=/bin/sed | ||
| _SEDEXT="/bin/sed -r" | ||
| _SORT=/bin/sort | ||
| _TOUCH=/bin/touch | ||
|
|
||
| else | ||
| echo "ERROR: OS not supported: ${OSTYPE}" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| ####################################################################### | ||
| # A simple compatibility wrapper around the mktemp command. | ||
| # | ||
| # Usage: | ||
| # $ make_temp_file [-d] [PREFIX] | ||
| # | ||
| # By default, creates a temporary file (use the -d option to | ||
| # create a directory). Takes an optional prefix argument that | ||
| # is used to construct the temporary file (or directory) name | ||
| # (defaults to some unspecified prefix if the argument is omitted). | ||
| # | ||
| # This script is compatible with Mac OS and GNU/Linux. | ||
| ####################################################################### | ||
|
|
||
| make_temp_file () { | ||
| local prefix | ||
| local _path_mktemp | ||
| local mktemp_arg | ||
| local temp_file | ||
| local return_code | ||
|
|
||
| # process command-line options (if any) | ||
| local OPTARG | ||
| local OPTIND | ||
| local local_opts= | ||
| while getopts "d" opt; do | ||
| case $opt in | ||
| d) | ||
| local_opts=-d | ||
| ;; | ||
| \?) | ||
| echo "ERROR: $FUNCNAME: Unrecognized option: -$OPTARG" >&2 | ||
| return 1 | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| # determine the prefix | ||
| shift $((OPTIND-1)) | ||
| if [ $# -eq 0 ]; then | ||
| prefix="temp" | ||
| else | ||
| prefix="$1" | ||
| fi | ||
|
|
||
| if [[ ${OSTYPE} = darwin* ]] ; then | ||
| _path_mktemp=/usr/bin/mktemp | ||
| # on Mac OS, mktemp takes a prefix | ||
| mktemp_arg="${prefix}" | ||
| elif [[ ${OSTYPE} = linux* ]] ; then | ||
| _path_mktemp=/bin/mktemp | ||
| # on Linux, mktemp takes a template | ||
| mktemp_arg="${prefix}.XXXXXXXX" | ||
| else | ||
| echo "ERROR: OS not supported: ${OSTYPE}" >&2 | ||
| return 1 | ||
| fi | ||
|
|
||
| # create temporary file | ||
| temp_file=$( ${_path_mktemp} ${local_opts} -t ${mktemp_arg} ) | ||
| return_code=$? | ||
| if [ $return_code -ne 0 ] ; then | ||
| echo "ERROR: $FUNCNAME: failed to make temp file" >&2 | ||
| return $return_code | ||
| fi | ||
|
|
||
| echo $temp_file | ||
| return 0 | ||
| } | ||
|
|
||
| ####################################################################### | ||
| # Prints a message to a log file. | ||
| # | ||
| # Usage: | ||
| # $ print_log_message LOG_MESSAGE LOG_FILE | ||
| # | ||
| # Computes a timestamp and prepends the timestamp to the message | ||
| # before appending the message to the log file. | ||
| # | ||
| # This script is compatible with Mac OS and GNU/Linux. | ||
| ####################################################################### | ||
|
|
||
| print_log_message () { | ||
|
|
||
| # local vars | ||
| local log_message | ||
| local log_file | ||
| local tstamp | ||
|
|
||
| # make sure there are two command-line arguments | ||
| if [ $# -ne 2 ]; then | ||
| echo "ERROR: $FUNCNAME: incorrect number of arguments: $# (2 required)" >&2 | ||
| return 2 | ||
| fi | ||
|
|
||
| # get the log message | ||
| log_message="$1" | ||
| log_file="$2" | ||
|
|
||
| # compute timestamp | ||
| tstamp=$( /bin/date -u +%Y-%m-%dT%TZ ) | ||
|
|
||
| # prepend timestamp to the log message | ||
| printf "%s %s\n" "$tstamp" "$log_message" >> "$log_file" | ||
| } |