Bash Template
2024-09-30 15:35:23
#/bin/bash
#set -x
set -e usage()
{
cat <<EOF
Usage:
`basename $` [OPTIONS] <non-option arguments> Purpose:
xxxxxxxxxxxx Description:
None Parameters: OPTIONS
-i image -- images, registry: by default
-p -- host port, by default <non-option arguments> Author:
anor@xxxx.com Revision:
-- Anor Initial Versiona EOF
exit
} ############################################
#declare options string including optional
#argument names separated from withsapce.
#g_opt_name includs optional arg names
#g_opt_<g_opt_name>, just holds optional arg value
#for example,
# Given optional args, -i, -p,
# the option arg variables respectivly should be as,
# g_opt_i
# g_opt_p
############################################
declare -a g_opt_names=(i: p:) ############################################
#init_args()
#initialize arguments before set them with
#command line values
############################################
init_args()
{
g_opt_i="registry:2" # the registry image from hub.docker.com
g_opt_p=
} ############################################
#parse_non_option_args()
#parse non option arguments here
############################################
parse_non_option_args()
{
#//TODD add your code to parse non-option arguments here. return ;
} ############################################
#main()
#add business logics in this [main] function
############################################
main()
{
#//TODD add your bussiness code here return ;
} #*******************************************************
########################################################
### Common functions here
########################################################
#*******************************************************
parse_args()
{ #check if showing usage
if [ "$1" == "--help" ]; then usage; fi init_args parse_options "$@" parse_non_option_args "$@"
}
parse_options()
{
local opt_names="${g_opt_names[@]}" local opt_string="$(echo -e "$opt_names" | tr -d '[[:space:]]')" log "opt_string=$opt_string" if [ -n "$opt_names" ]; then
while getopts $opt_string opt
do
for i in "${g_opt_names[@]}"
do
local opt_name=${i::}
local opt_var_name=g_opt_$opt_name
case $opt in
$opt_name )
if [ -z "$OPTARG" ]
then
eval $opt_var_name=
else
eval $opt_var_name=\"$OPTARG\"
fi
log "$opt_var_name=${!opt_var_name}"
;;
h ) usage ;;
\?) usage ;;
esac
done
done
fi
} #Purpose:
# echo log message
#
#Parameters:
# @.... -- log messages
#
log()
{
echo "[`date +'%Y-%m-%d %H:%M:%S'` $0]" "${@:1}"
} _main()
{ #parse optional
parse_args "$@" #main routine
main "$@" #successful log
log "Done!"
} _main "$@"
最新文章
- Lintcode 150.买卖股票的最佳时机 II
- MySQL数据库备份--mysqldump用法
- 《Python核心编程》18.多线程编程(二)
- redis-3.2.5 make 报错
- 在android 中开发java.net.SocketException: socket failed: EACCES (Permission denied) 报错
- arp命令
- PHP操作mysql数据库:[2]查询数据听语音
- binarySearch二分查找——Javascript实现
- 搭建golang的beego注意事项
- C#整理7——函数
- 百度ueditor富文本编辑器的使用
- use EXPORT和use EXPORT_OK
- shell 字符串包含
- 从零一起学Spring Boot之LayIM项目长成记(六)单聊群聊的实现
- android:shape 设置圆形
- 华为QUIDWAY系列路由器的单臂路由配置案例
- SpringIOC框架详解
- User模型扩展和自定义
- Github介绍
- 添加JavaDoc
热门文章
- 实现静默安装APK的方法
- Java 并发 —— 读写锁(ReadWriteLock)
- Lightoj 1140(数位DP)
- Are you sure you want to continue connecting etc ssh ssh_config StrictHostKeyChecking no
- 【192】PowerShell 相关知识
- asp.net mvc razer
- 10款Web开发最佳的Python框架
- CodeForces 722C Destroying Array (并查集)
- [笔记]如何将已存在的JAVA添加到当前ECLIPSE JAVA工程中
- TransposonPSI——转座子分析的入门自学