sh 简单的bash shell脚本模板。有两个版本:1)简单的基于env var的选项,以及2)添加了命令行参数解析

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 简单的bash shell脚本模板。有两个版本:1)简单的基于env var的选项,以及2)添加了命令行参数解析相关的知识,希望对你有一定的参考价值。

#!/bin/bash -e
# A SHORT DESCRIPTION OF YOUR SCRIPT GOES HERE
###############################################################################
set -e          # exit on command errors (so you MUST handle exit codes properly!)
set -E          # pass trap handlers down to subshells
set -o pipefail # capture fail exit codes in piped commands
#set -x         # execution tracing debug messages

# Error handler
on_err() {
	echo ">> ERROR: $?"
	FN=0
	for LN in "${BASH_LINENO[@]}"; do
		[ "${FUNCNAME[$FN]}" = "main" ] && break
		echo ">> ${BASH_SOURCE[$FN]} $LN ${FUNCNAME[$FN]}"
		FN=$(( FN + 1 ))
	done
}
trap on_err ERR

# Exit handler
declare -a EXIT_CMDS
add_exit_cmd() { EXIT_CMDS+="$*;  "; }
on_exit(){ eval "${EXIT_CMDS[@]}"; }
trap on_exit EXIT

# Get command info
CMD_PWD=$(pwd)
CMD="$0"
CMD_DIR="$(cd "$(dirname "$CMD")" && pwd -P)"

# Defaults and command line options
[ "$VERBOSE" ] ||  VERBOSE=
[ "$DEBUG" ]   ||  DEBUG=
[ "$THING" ]   ||  THING=123	# assuming that you have a thing
>>>> PUT YOUR ENV VAR DEFAULTS HERE <<<<

# Basic helpers
out() { echo "$(date +%Y%m%dT%H%M%SZ): $*"; }
err() { out "$*" 1>&2; }
vrb() { [ ! "$VERBOSE" ] || out "$@"; }
dbg() { [ ! "$DEBUG" ] || err "$@"; }
die() { err "EXIT: $1" && [ "$2" ] && [ "$2" -ge 0 ] && exit "$2" || exit 1; }

# Show help function to be used below
show_help() {
	awk 'NR>1{print} /^(###|$)/{exit}' "$CMD"
	echo "USAGE: $(basename "$CMD") [arguments]"
	echo "ARGS:"
	MSG=$(awk '/^NARGS=-1; while/,/^esac; done/' "$CMD" | sed -e 's/^[[:space:]]*/  /' -e 's/|/, /' -e 's/)//' | grep '^  -')
	EMSG=$(eval "echo \"$MSG\"")
	echo "$EMSG"
}

# Parse command line options (odd formatting to simplify show_help() above)
NARGS=-1; while [ "$#" -ne "$NARGS" ]; do NARGS=$#; case $1 in
	# SWITCHES
	-h|--help)      # This help message
		show_help; exit 1; ;;
	-d|--debug)     # Enable debugging messages (implies verbose)
		DEBUG=$(( DEBUG + 1 )) && VERBOSE="$DEBUG" && shift && echo "#-INFO: DEBUG=$DEBUG (implies VERBOSE=$VERBOSE)"; ;;
	-v|--verbose)   # Enable verbose messages
		VERBOSE=$(( VERBOSE + 1 )) && shift && echo "#-INFO: VERBOSE=$VERBOSE"; ;;
	# PAIRS
	-t|--thing)     # Set a thing to a value (DEFAULT: $THING)
		shift && THING="$1" && shift && vrb "#-INFO: THING=$THING"; ;;
	*)
		break;
esac; done

[ "$DEBUG" ]  &&  set -x

###############################################################################

# Validate some things
#TODO: You will probably want to change this but this is an example of simple params validation
[ $# -gt 0 -a -z "$THING" ]  &&  THING="$1"  &&  shift
[ "$THING" ]  ||  die "You must provide some thing!"
[ $# -eq 0 ]  ||  die "ERROR: Unexpected commands!"

>>>> PUT YOUR SCRIPT HERE <<<<
#!/bin/bash -e
# A SHORT DESCRIPTION OF YOUR SCRIPT GOES HERE
# USAGE:
#    DESCRIPTION OF ENV VARS HERE
###############################################################################
set -e          # exit on command errors (so you MUST handle exit codes properly!)
set -o pipefail # capture fail exit codes in piped commands
#set -x         # execution tracing debug messages

# Get command info
CMD_PWD=$(pwd)
CMD="$0"
CMD_DIR="$(cd "$(dirname "$CMD")" && pwd -P)"

# Defaults and command line options
[ "$VERBOSE" ]  ||  VERBOSE=
[ "$DEBUG" ]    ||  DEBUG=
[ "$THING" ]    ||  THING=123	# assuming that you have a thing
>>>> PUT YOUR ENV VAR DEFAULTS HERE <<<<

# Basic helpers
out() { echo "$(date +%Y%m%dT%H%M%SZ): $*"; }
err() { out "$*" 1>&2; }
vrb() { [ ! "$VERBOSE" ] || out "$@"; }
dbg() { [ ! "$DEBUG" ] || err "$@"; }
die() { err "EXIT: $1" && [ "$2" ] && [ "$2" -ge 0 ] && exit "$2" || exit 1; }
usage() { [ "$0" = "bash" ] || sed '2,/^##/p;d' "$0"; echo "$*"; exit 1; }

[ "$DEBUG" ]  &&  set -x

###############################################################################

# Validate some things
[ "$1" = "--help" -o "$1" = "-h" ]  &&  usage ""

>>>> PUT YOUR SCRIPT HERE <<<<

以上是关于sh 简单的bash shell脚本模板。有两个版本:1)简单的基于env var的选项,以及2)添加了命令行参数解析的主要内容,如果未能解决你的问题,请参考以下文章

shell命令中用source 和sh(或者bash)执行脚本的区别,以及export的作用

shell命令中用source 和sh(或者bash)执行脚本的区别,以及export的作用

Mac上实现一个简单的shell脚本

Shell脚本简单实例

shell sh bash 概念

Shell脚本