sh简单的bashshell脚本模板。有两个版本:1)简单的基于envvar的选项,以及2)添加了命令行参数解析(代码片段)

author author     2022-12-21     335

关键词:

#!/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>1print /^(###|$)/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检查bashshell脚本中是否存在输入参数(代码片段)

查看详情

linux多种提交作业的指令

...执行shell脚本:./test.sh方法二:以绝对路径的方式去执行bashshell脚本:/home/user1/test.sh方法三:直接使用bash或sh来执行bashshell脚本:sh test.shbashtest.sh。该方法执行脚本,可以不必事先设定shell的执行权限,甚至都不用写shell文件中... 查看详情

linuxshell脚本执行命令详解

...  linuxshell脚本执行方法二:以绝对路径的方式去执行bashshell脚本:  /data/shell/hello.sh  linuxshell脚本执行方法三:直接使用bash或sh来执行bashshell脚本:  cd/data/shell  bashhello.sh  或  cd/data/shell  shhello.sh  注意... 查看详情

如何查看tomcatjdk版本

首先,TomcatTomcat本身提供了查看版本的脚本命令:version就在Tomcat的安装目录的bin子目录下,有两个文件:·version.bat--Windows下的批处理脚本·version.sh--Linux下的Shell脚本执行脚本就可以查看Tomcat的版本了。只需要使用cmd命令行,进... 查看详情

linux脚本如何一键授权并执行

...作目录)执行shell脚本方法二:以绝对路径的方式去执行bashshell脚本方法三:直接使用bash或sh来执行bashshell脚本方法四:在当前的shell环境中执行bashshell脚本参考技术Alinux下的NetworkManager服务,解决servicenetworkrestart闲置网卡报错问... 查看详情

linux设置开机自启动脚本的最佳方式

...配置文件中搜集shell的设置。(2)/etc/bashrc:为每一个运行bashshell的用户执行此文件.当bashshell被打开时,该文件被读取(即每次新开一个终端,都会执行bashrc)。(3)~/.bash_profile:每个用户都可使用该文件输入专用于自己使用的shell... 查看详情

linux中编写一个shell脚本将指定目录中的文件的文件全名改为大写,但是命令执行完,shell

...已经改了,但是回到图形界面,并没有改,这是为什么?bashshell脚本的方法有多种,现在作个小结。假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限。方法一:切换到shell脚本所在的目录... 查看详情

在 bash shell 脚本中传播所有参数

】在bashshell脚本中传播所有参数【英文标题】:Propagateallargumentsinabashshellscript【发布时间】:2011-03-1213:46:20【问题描述】:我正在编写一个调用另一个脚本的非常简单的脚本,我需要将参数从我当前的脚本传播到我正在执行的脚... 查看详情

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

参考技术A如何实现一个简单的shell脚本呢?(这里用到的命令工具是iTerm2)直接上开始吧cd到目标文件夹:$cdtest新建脚本$touch123.sh双击123.sh,里面写入:mkdirshellcdshelltouch123.txt然后保存一下,此刻已经完成了一个简单的脚本(123.sh... 查看详情

如何执行shell脚本其中一条命令

...必须加上./才可执行。方法二:以绝对路径的方式去执行bashshell脚本:复制代码代码如下:/data/shell/hello.sh方法三:直接使用bash或sh来执行bashshell脚本:复制代码代码如下:cd/data/shellbashhello.sh或复制代码代码如下:cd/data/shellshhello.sh... 查看详情

sh模板与bash脚本的一些便利设置。(代码片段)

查看详情

sh一个简单的备份脚本。(代码片段)

查看详情

sh简单的bash脚本(代码片段)

查看详情

快速循环的简单 PERL 脚本

】快速循环的简单PERL脚本【英文标题】:SimplePERLscripttoloopveryquickly【发布时间】:2014-11-2809:10:43【问题描述】:我正在尝试让perl脚本快速循环(在Solaris中)。我有这样的事情:#!/bin/perlwhile(\'true\')usestrict;usewarnings;useTime::HiRes;sys... 查看详情

linux运行一个可执行文件,出现line1:syntaxerror:unexpected"("的错误

...一种是给予权限,使用shshell.sh或./shell.sh的方式,一种是bashshell.sh的方式查看脚本执行过程-x[root@localhostsrc]#sh-x1.sh+echo123123exit查看脚本语法是否有错误-n[root@localhostsrc]#sh-n1.sh1.sh:line5:syntaxerror:unexpectedendoffile 查看详情

如何hook某一个shell命令

...必须加上./才可执行。方法二:以绝对路径的方式去执行bashshell脚本:复制代码代码如下:/data/shell/hello.sh方法三:直接使用bash或sh来执行bashshell脚本:复制代码代码如下:cd/data/shellbashhello.sh或复制代码代码如下:cd/data/shellshhello.sh... 查看详情

sh简单的ios构建脚本(代码片段)

查看详情

sh简单的ios构建脚本(代码片段)

查看详情