From 882172548d8873c4ca38eb46b28396c834999a07 Mon Sep 17 00:00:00 2001 From: lework Date: Mon, 29 Jul 2019 10:00:04 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0bash=E8=84=9A=E6=9C=AC?= =?UTF-8?q?=E7=9A=84=E6=A8=A1=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- shell/template.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 shell/template.sh diff --git a/shell/template.sh b/shell/template.sh new file mode 100644 index 0000000..69a2be6 --- /dev/null +++ b/shell/template.sh @@ -0,0 +1,58 @@ +#!/bin/env bash +################################################################### +#Script Name : +#Description : +#Args : +#Update Date : +#Author : lework +#Email : lework@yeah.net +################################################################### + +set -o errexit # Exit on most errors (see the manual) +set -o errtrace # Make sure any error trap is inherited +set -o nounset # Disallow expansion of unset variables +set -o pipefail # Use last non-zero exit code in a pipeline + + +TAG="CMD" +LOG_PATH="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/logs" +LOG_FILE="$LOG_PATH/example_`date +"%Y%m%d"`.log" +HIDE_LOG=true + +function log() { + [ ! -d "$LOG_PATH" ] && mkdir -p $LOG_PATH + if [ $HIDE_LOG ]; then + echo -e "[`date +"%Y/%m/%d:%H:%M:%S %z"`] [`whoami`] [$TAG] $@" >> $LOG_FILE + else + echo "[`date +"%Y/%m/%d:%H:%M:%S %z"`] [`whoami`] [$TAG] $@" | tee -a $LOG_FILE + fi +} + +function script_trap_err() { + local exit_code=1 + + # Disable the error trap handler to prevent potential recursion + trap - ERR + + # Consider any further errors non-fatal to ensure we run to completion + set +o errexit + set +o pipefail + + log "[E] ERROR" + + exit "$exit_code" +} + +function script_trap_exit() { + log "[I] shell exec done." +} + +function main() { + trap script_trap_err ERR + trap script_trap_exit EXIT + + log "[I] shell start" + +} + +main "${@}"