#!/bin/bash
set -u
#==============================================================================
# Copyright and license info is available in the LICENSE file included with
# the Server Deployment Package (SDP), and also available online:
# https://swarm.workshop.perforce.com/projects/perforce-software-sdp/view/main/LICENSE
#------------------------------------------------------------------------------
#==============================================================================
# Declarations and Environment
declare ThisScript=${0##*/}
declare Version=1.0.0
declare ThisUser=
declare ThisHost=${HOSTNAME%%.*}
declare -i ErrorCount=0
declare -i Debug=${DEBUG:-0}
declare -i SilentMode=0
declare -i WarningCount=0
declare Log=
declare H1="=============================================================================="
declare H2="------------------------------------------------------------------------------"
declare SDPInstance=${SDP_INSTANCE:-}
declare SDPRoot=${SDP_ROOT:-/p4}
declare SDPInstanceVars=
declare -i NO_OP=0
declare -i VERBOSITY=3
#==============================================================================
# Local Functions
function msg () { echo -e "$*"; }
function msgn () { echo -e -n "$*"; }
function dbg () { [[ "$Debug" -eq 0 ]] || msg "DEBUG: $*"; }
function errmsg () { msg "\\nError: ${1:-Unknown Error}\\n"; ErrorCount+=1; }
function warnmsg () { msg "\\nWarning: ${1:-Unknown Warning}\\n"; WarningCount+=1; }
function bail () { errmsg "${1:-Unknown Error}"; exit "${2:-1}"; }
#------------------------------------------------------------------------------
# Function: terminate
function terminate
{
# Disable signal trapping.
trap - EXIT SIGINT SIGTERM
dbg "$ThisScript: EXITCODE: $ErrorCount"
# Stop logging.
[[ "$Log" == off ]] || msg "\\nLog is: $Log\\n${H1}"
# With the trap removed, exit with the error count.
exit "$ErrorCount"
}
#------------------------------------------------------------------------------
# Function: usage (required function)
#
# Input:
# $1 - style, either -h (for short form) or -man (for man-page like format).
# The default is -h.
#
# $2 - error message (optional). Specify this if usage() is called due to
# user error, in which case the given message displayed first, followed by the
# standard usage message (short or long depending on $1). If displaying an
# error, usually $1 should be -h so that the longer usage message doesn't
# obscure the error message.
#
# Sample Usage:
# usage
# usage -h
# usage -man
# usage -h "Incorrect command line usage."
#------------------------------------------------------------------------------
function usage
{
declare style=${1:--h}
declare usageErrorMessage=${2:-Unset}
if [[ "$usageErrorMessage" != Unset ]]; then
msg "\\n\\nUsage Error:\\n\\n$usageErrorMessage\\n\\n"
fi
msg "USAGE for $ThisScript v$Version:
$ThisScript EDITME <required> literal [-optional] [-i <sdp_instance>] [-L <log>] [-si] [-n] [-d|-D]
or
$ThisScript [-h|-man|-V]
"
if [[ $style == -man ]]; then
echo -e "
DESCRIPTION: EDITME
OPTIONS:
-i <sdp_instance>
Specify the SDP Instance. If not specified, the \$SDP_INSTANCE variable is
referenced, otherwise the default is '1'.
-L <log>
Specify the path to a log file, or the special value 'off' to disable
logging. By default, all output (stdout and stderr) goes to
EDITME_DEFAULT_LOG
NOTE: This script is self-logging. That is, output displayed on the screen
is simultaneously captured in the log file. Do not run this script with
redirection operators like '> log' or '2>&1', and do not use 'tee.'
-si Operate silently. All output (stdout and stderr) is redirected to the log
only; no output appears on the terminal. This cannot be used with
'-L off'.
EDITME: This is useful when running from cron, as it prevents automatic
email from being sent by cron directly, as it does when a script called
from cron generates any output. This script is then responsible for
email handling, if any is to be done.
-n No-Op. Prints commands instead of running them.
-D Set extreme debugging verbosity.
HELP OPTIONS:
-h Display short help message
-man Display man-style help message
-V Display version info for this script and its libraries.
FILES:
EXAMPLES:
SEE ALSO:
"
fi
exit 2
}
#==============================================================================
# Command Line Processing
declare SampleArgument=""; ### EDITME
declare -i shiftArgs=0
set +u
while [[ $# -gt 0 ]]; do
case $1 in
(-s) SampleArgument="$2"; shiftArgs=1;; ### EDITME
(-h) usage -h;;
(-man) usage -man;;
(-V) show_versions; exit 1;;
(-L) Log="$2"; shiftArgs=1;;
(-si) SilentMode=1;;
(-n) export NO_OP=1;;
(-D) set -x;; # Debug; use 'set -x' mode.
(*) usage -h "Unknown arg ($1).";;
esac
# Shift (modify $#) the appropriate number of times.
shift; while [[ $shiftArgs -gt 0 ]]; do
[[ $# -eq 0 ]] && usage -h "Incorrect number of arguments."
shiftArgs=$shiftArgs-1
shift
done
done
set -u
source "$SDPRoot/common/bin/p4_vars" "$SDPInstance" ||\
bail "Could not do: source \"$SDPRoot/common/bin/p4_vars\" \"$SDPInstance\""
declare BASH_LIBS="$P4CLIB/librun.sh"
for bash_lib in $BASH_LIBS; do
# shellcheck disable=SC1090
source "$bash_lib" ||\
bail "Could not do: source \"$bash_lib\""
done
#==============================================================================
# Command Line Verification
[[ "$SilentMode" -eq 1 && "$Log" == off ]] && \
usage -h "Cannot use '-si' with '-L off'."
[[ -n "$Log" ]] || \
Log="${LOGS:-${HOME:-/tmp}}/${ThisScript%.sh}.$(date +'%Y%m%d-%H%M%S').log"
#==============================================================================
# Main Program
trap terminate EXIT SIGINT SIGTERM
declare -i OverallReturnStatus=0
if [[ "$Log" != off ]]; then
touch "$Log" || bail "Couldn't touch log file [$Log]."
# Redirect stdout and stderr to a log file.
if [[ "$SilentMode" -eq 0 ]]; then
exec > >(tee "$Log")
exec 2>&1
else
exec >"$Log"
exec 2>&1
fi
msg "${H1}\\Log is: $Log\\n"
fi
run "ls /tmp" "List temp area."
echo "foo"
echo "bar" >&2
run "ls" "Sample Arg specified with '-s <arg>' is [$SampleArgument]. Directory List:" 1 1
run "/bin/sleep 1" "Taking a short nap." 1 0
# Illustrate stringing together a series of commands using a '|'.
# run can handle this.
run "ls -lrt|grep \"Dec 16\"|head -2|tail -1" "Showing last file:" 1 1
warnmsg "Sample WarningMsg 1"
errmsg "Sample Error Message 2"
warnmsg "Sample WarningMsg 3"
errmsg "Sample Error Message 4"
rrun localhost "sleep 2\\nls -lrt /tmp/" "Doing an ls on simulated remote host." 1 1 0
rrun localhost "ls -l /tmp/*" "Grepping remote output for /tmp/hello" 0 0 "/tmp/hello" ||\
warnmsg "/tmp/hello was not found in remote output."
if [[ $OverallReturnStatus -eq 0 ]]; then
msg "${H1}\\nAll processing completed successfully.\\n"
else
msg "${H1}\\nProcessing completed, but with errors. Scan above output carefully.\\n"
fi
# Illustrate using $SECONDS to display runtime of a script.
msg "Time: That took $((SECONDS/3600)) hours $((SECONDS%3600/60)) minutes $((SECONDS%60)) seconds.\\n"
# See the terminate() function, which is where this script actually exits.
exit "$ErrorCount"
| # | Change | User | Description | Committed | |
|---|---|---|---|---|---|
| #2 | 31860 | C. Thomas Tyler | Importing coding standard updates from shelf in legacy structure. | ||
| #1 | 31397 | C. Thomas Tyler | Populate -b SDP_Classic_to_Streams -s //guest/perforce_software/sdp/...@31368. | ||
| //guest/perforce_software/sdp/dev/Server/Unix/p4/common/bin/templates/template.sh | |||||
| #2 | 27722 | C. Thomas Tyler |
Refinements to @27712: * Resolved one out-of-date file (verify_sdp.sh). * Added missing adoc file for which HTML file had a change (WorkflowEnforcementTriggers.adoc). * Updated revdate/revnumber in *.adoc files. * Additional content updates in Server/Unix/p4/common/etc/cron.d/ReadMe.md. * Bumped version numbers on scripts with Version= def'n. * Generated HTML, PDF, and doc/gen files: - Most HTML and all PDF are generated using Makefiles that call an AsciiDoc utility. - HTML for Perl scripts is generated with pod2html. - doc/gen/*.man.txt files are generated with .../tools/gen_script_man_pages.sh. #review-27712 |
||
| #1 | 26745 | Robert Cowham | Create templates sub=folder to tidy up /p4/common/bin | ||
| //guest/perforce_software/sdp/dev/Server/Unix/p4/common/bin/template.sh | |||||
| #13 | 26391 | C. Thomas Tyler |
Added explicit initialization for P4U_LOG to template.sh. Made some shellcheck compliance tweaks. |
||
| #12 | 25785 | C. Thomas Tyler | Cosmetic doc typo fix in template.sh | ||
| #11 | 25545 | C. Thomas Tyler |
Enhanced log file handling in bash shell script template, using SDP $LOGS variable by default. Shellcheck v0.6.0 compliance change. |
||
| #10 | 22628 | C. Thomas Tyler |
Fixed minor order-of-processing bug resulting in a harmless error appearing at the end of script processing as cleanTrash() was called to clean garbage files. The run() function was called to clean garbage files/dirs just as a directory that function depended on got cleaned up. The fix was applied to scripts that used libcore.sh, including the template.sh template script. Also corrected comments in p4u_env.sh. Bypassing pre-commit review as this has been well tested. #review-22629 |
||
| #9 | 20705 | C. Thomas Tyler |
Enhanced standard usage() function to also handle an optional usage error message. |
||
| #8 | 20663 | C. Thomas Tyler |
Updates to auxiliary files, libcore.sh and template.sh: * Added run() and rrun() functions, new and improved versions of runCmd and runRemoteCmd(), leaving original functions (mostly) as is for backward compatibilty. |
||
| #7 | 20382 | C. Thomas Tyler |
Tweaks to supplemental (non-core SDP) scripts p4u_env.sh, libcore.sh, template.sh: * Fixed bug relying on $USER, which is not guaranteed to be defined, preventing errors in unusual situations where it is not defined. * Streamlined temp file management, adding new P4U_TMPDIR directory, which is cleaned up automatically. It uses /dev/shm if available, otherwise 'mktemp -d'. * runCmd() and runRemoteCmd() now clean up temp files as they goes. * Added new 'captureOutputFlag' parameter to runRemoteCmd(), with similar semantics as with the same parameter in runCmd(). * Updated template.sh to illustrate new parameter in runRemoteCmd(). |
||
| #6 | 16784 | C. Thomas Tyler |
Routine Merge Down to dev from main using: p4 -s merge -n -b perforce_software-sdp-dev |
||
| #5 | 16029 | C. Thomas Tyler |
Routine merge to dev from main using: p4 merge -b perforce_software-sdp-dev |
||
| #4 | 15136 | C. Thomas Tyler | Routine merge down using 'p4 merge -b perforce_software-sdp-dev'. | ||
| #3 | 13582 | C. Thomas Tyler |
Updated template.sh and associated bash libraries. Added show_versions() standard function and a standard '-V' flag to access it. Added version value definitions to all bash library files. |
||
| #2 | 12169 | Russell C. Jackson (Rusty) |
Updated copyright date to 2015 Updated shell scripts to require an instance parameter to eliminate the need for calling p4master_run. Python and Perl still need it since you have to set the environment for them to run in. Incorporated comments from reviewers. Left the . instead of source as that seems more common in the field and has the same functionality. |
||
| #1 | 10638 | C. Thomas Tyler | Populate perforce_software-sdp-dev. | ||
| //guest/perforce_software/sdp/main/Server/Unix/p4/common/bin/template.sh | |||||
| #1 | 10148 | C. Thomas Tyler | Promoted the Perforce Server Deployment Package to The Workshop. | ||