shell脚本处理长参数的模板
shell脚本处理长参数的模板
一个shell模板,处理命令行参数,支持长短参数:
#!/bin/bash
#
# FILE: kvm-clone-v2.sh
#
# DESCRIPTION: Clone a RHEL5.4/RHEL6 kvm guest on ubuntu14.04 host superv.
# This shell is used for cloning RHEL5.4 or RHEL6.x KVM guest.
# Note this shell is only tested for host OS Ubuntu14.04 and RHEL6.4.
#
# KVM is short for Kernel-based Virtual Machine and makes use of
# hardware virtualization. You need a CPU that supports hardware
# virtualization, e.g. Intel VT or AMD-V.
#
# NOTES: This requires GNU getopt.
# I do not issue any guarantee that this will work for you!
#
# COPYRIGHT: (c) 2015-2016 by the ZhangLiang
#
# LICENSE: Apache 2.0
#
# ORGANIZATION: PepStack (pepstack.com)
#
# CREATED: 2015-05-22 12:34:00
#
#=======================================================================
_file=$(readlink -f $0)
_dir=$(dirname $_file)
. $_dir/common.sh
# Treat unset variables as an error
set -o nounset
__ScriptVersion="2015.05.22"
__ScriptName="kvm-clone-v2.sh"
#-----------------------------------------------------------------------
# FUNCTION: usage
# DESCRIPTION: Display usage information.
#-----------------------------------------------------------------------
usage() {
cat << EOT
Usage : ${__ScriptName} CFGFILE [OPTION] ...
Create a virtual machine from given options.
Options:
-h, --help Display this message
-V, --version Display script version
-v, --verbose
-o, --origver=ORIGVER Origin vm name with version: rhel5_4 | rhel6_4
-D, --disksize=DISKSIZE Origin vm disk size: compact|medium|large
-p, --path-prefix=PATH Path prefix of vm
-m, --memsize=SIZEMB Memory size of vm by MB: 8192
-c, --vcpus=VCPUS Number of virtual cpu cores: 4
-n, --vmname=VMNAME Given name of vm
-H, --domain<DOMAIN> Optional hostname suffix of vm
-i, --ipv4=IPADDR Static ipv4 addr of vm if used
-S, --supervisor=SUPERVISOR Supervisor of vm: rhel6.4 or ubuntu14.04
-G, --gateway=GATEWAY Gateway ipv4 address
-T, --iftype=IFTYPE Network type: bridge or default
-B, --broadcast=BCAST Broadcast inet addr
-M, --netmask=MASK Net mask address, default: 255.255.255.0
Exit status:
0 if OK,
!=0 if serious problems.
Example:
1) Use short options to create vm:
$ sudo $__ScriptName ../conf/kvm-origin.cfg -o rhel6_4 -D compact -p el6 -m 2048 -c 2 -n vm-test2 -H pepstack.com -i 192.168.122.61 -S ubuntu14.04 -G 192.168.122.1 -B 192.168.122.255 -M 255.255.255.0
2) Use long options to create vm:
$ sudo $__ScriptName ../conf/kvm-origin.cfg --origver=rhel6_4 --disksize=compact --path-prefix=el6 --memsize=2048 --vcpus=2 --vmname=vm-test3 --domain=pepstack.com --ipv4=192.168.122.63 --supervisor=ubuntu14.04 --gateway=192.168.122.1 --broadcast=192.168.122.255 --netmask=255.255.255.0
Report bugs to 350137278@qq.com
EOT
} # ---------- end of function usage ----------
if [ $# -eq 0 ]; then usage; exit 1; fi
ABSDIR=$(real_path $(dirname $0))
CFGFILE=
VMNAME=
DOMAIN=
IPADDR=
PATHPREFIX=
GATEWAY=
BDCAST=
NETMASK="255.255.255.0"
VERBOSE=false
SIZEMB=8192
VCPUS=4
ORIGVER="rhel6_4"
DISKSIZE="compact"
VMORIG="$ORIGVER:$DISKSIZE"
SUPERVISOR="ubuntu14.04"
# parse options:
RET=`getopt -o hVvo:D:p:m:c:n:H::i:S:G:T:B:M: \
--long help,version,verbose,origver:,disksize:,path-prefix:,memsize:,\
vcpus:,vmname:,domain::,ipv4:,supervisor:,gateway:,\
iftype:,broadcast:,netmask:\
-n ' * ERROR' -- "$@"`
if [ $? != 0 ] ; then echoerror "$__ScriptName exited with doing nothing." >&2 ; exit 1 ; fi
# Note the quotes around $RET: they are essential!
eval set -- "$RET"
# set option values
while true; do
case "$1" in
-h | --help ) usage; exit 1;;
-v | --verbose ) VERBOSE=true; shift ;;
-V | --version ) echoinfo "$(basename $0) -- version $__ScriptVersion"; exit 1;;
-o | --origver ) ORIGVER=$2
echoinfo "origin: $ORIGVER"
shift 2 ;;
-D | --disksize ) DISKSIZE=$2
echoinfo "origin size: $DISKSIZE"
shift 2 ;;
-p | --path-prefix ) PATHPREFIX=$2
echoinfo "subdir: $PATHPREFIX"
shift 2 ;;
-n | --vmname) VMNAME=$2
echoinfo "new vm name: $VMNAME"
shift 2 ;;
-H | --domain)
# domain-suffix has an optional argument. as we are in quoted mode,
# an empty parameter will be generated if its optional argument is not found.
case "$2" in
"" ) echowarn "--domain, no argument"; shift 2 ;;
* ) DOMAIN="$2" ; echoinfo "domain: $DOMAIN"; shift 2 ;;
esac ;;
-i | --ipv4) IPADDR=$2
echoinfo "static ipv4: $IPADDR"
shift 2 ;;
-m | --memsize ) SIZEMB=$2
echoinfo "memory: $SIZEMB mb"
shift 2 ;;
-c | --vcpus ) VCPUS=$2
echoinfo "cpu cores: $VCPUS"
shift 2 ;;
-S | --supervisor ) SUPERVISOR=$2
echoinfo "supervisor: $SUPERVISOR"
shift 2;;
-G | --gateway ) GATEWAY=$2
echoinfo "gateway: $GATEWAY"
shift 2 ;;
-T | --iftype ) IFTYPE=$2
echoinfo "network type: $IFTYPE"
shift 2 ;;
-B | --broadcast ) BDCAST=$2
echoinfo "broad cast: $BDCAST"
shift 2 ;;
-M | --netmask) NETMASK=$2
echoinfo "netmask: $NETMASK"
shift 2 ;;
-- ) shift; break ;;
* ) echoerror "internal error!" ; exit 1 ;;
esac
done
# config file must provided with remaining argument
for arg do
CFGFILE=$(real_path $(dirname $arg))'/'$(basename $arg)
done
if [ -f $CFGFILE ]; then
echoinfo "Config file: $CFGFILE"
else
echoerror "Config file not found: $CFGFILE"
exit 3
fi
##################### THIS IS ONLY A TEMPLATE SHELL FILE #####################
shell脚本处理长参数的模板的更多相关文章
- c++11变长参数函数模板
By francis_hao Mar 25,2018 一个最简单的实例大概是这个样子: #include <iostream>using namespace std; /*变长参 ...
- [Python]在python中调用shell脚本,并传入参数-02python操作shell实例
首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数 test_shell ...
- Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件
本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import ...
- shell脚本添加实例化参数
通过shell脚本给GMP系统添加一个环境变量参数dateSwitchTimeInterval 1. insert.sh #!/bin/sh . ~/apphome/aic_export.sh #连接 ...
- shell脚本学习- 传递参数
跟着RUNOOB网站的教程学习的笔记 我们可以在执行shell脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n代表一个数字,1为执行脚本的第一参数,2为执行脚本的第二个参数,以此类推... 实 ...
- Shell脚本之三 传递参数
我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推-- 实例 向脚本传递三个参数,并分 ...
- shell脚本获取的参数
$# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell脚本的第一个参数 $2 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表
- 向shell脚本中传入参数
写一个 程序名为 test.sh 可带参数为 start 和 stop 执行 test.sh start执行 start 内容的代码 执行 test.sh stop 执行 stop 内 ...
- C++11的模板新特性-变长参数的模板
这个特性很赞,直接给例子吧,假如我要设计一个类,CachedFetcher内部可能使用std::map也可能使用std::unordered_map,也可能是其它的map,怎么设计呢?没有C++11变 ...
随机推荐
- OpenResty 自定义 access_log 格式
定义access log的format是 Nginx已经提供的功能,有了 ngx_lua 之后就可以更灵活的记录请求相关的信息,而不仅仅拘泥于 Nginx的内置变量了,可以自定义一些格式和变量来存储结 ...
- Dynamics CRM 本地插件注册器连CRMAn unsecured or incorrectly secured fault was received from the other party
今天遇到个问题,在本地打开插件注册器连接到远程CRM服务器时报如下问题 但我在CRM服务器上连接注册器是可以打开的,所以不存在账号权限这类的问题了(当然我用的是超管的账号也不可能存在),最后查询得知是 ...
- NLP系列(1)_从破译外星人文字浅谈自然语言处理基础
作者:龙心尘 &&寒小阳 时间:2016年1月. 出处: http://blog.csdn.net/longxinchen_ml/article/details/50543337 ht ...
- android混淆那些坑
ProGuard简介 在最新的Android Studio 2.2.2版本创建的Android工程中,module中的build.gradle有如下一段配置.这里的minifyEnabled即用来控制 ...
- 基于BaseAdapter的Listview小Demo
ListView是android开发中比较常用的控件, 其中适配器模式可以选择: ArrayAdapter:简单易用,通常用于将数组或者List集合的读个包值封装成多个列表项 SimpleAdapte ...
- springMVC源码分析--动态样式ThemeResolver(一)
Spring MVC中通过ThemeSource接口来提供对动态更换样式的支持,并提供了ResourceBundleThemeSource这个具体实现类来提供通过properties配置文件对them ...
- ROS_Kinetic_27 在ROS中使用Cartographer进行SLAM
ROS_Kinetic_27 在ROS中使用Cartographer进行SLAM Cartographer是谷歌新開源的通用的2D和3D定位與構圖同步的SLAM工具,並提供ROS接口. 论文Real- ...
- EBS各个应用简称
模块全称 Banking Center 模块简称 FPT 服务器目录 FPT_TOP Billing Connect CUE CUE_TOP CADView-3D DDD DDD_TOP CPG ...
- Android开发学习之路--React-Native之初体验
近段时间业余在学node.js,租了个阿里云准备搭建后端,想用node.js,偶尔得知react-native可以在不同平台跑,js在iOS和android上都可以运行ok,今天就简单学习下rea ...
- <<精通iOS开发>>第14章例子代码小缺陷的修复
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 首先推荐大家看这本书,整本书逻辑非常清晰,代码如何从无到有,到 ...