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脚本处理长参数的模板的更多相关文章

  1. c++11变长参数函数模板

    By francis_hao    Mar 25,2018   一个最简单的实例大概是这个样子: #include <iostream>using namespace std; /*变长参 ...

  2. [Python]在python中调用shell脚本,并传入参数-02python操作shell实例

    首先创建2个shell脚本文件,测试用. test_shell_no_para.sh 运行时,不需要传递参数 test_shell_2_para.sh 运行时,需要传递2个参数  test_shell ...

  3. Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件

    本文通过Java代码调用Shell脚本并传入参数实现DB2数据库表导出到文件,代码如下: import java.io.File; import java.io.IOException; import ...

  4. shell脚本添加实例化参数

    通过shell脚本给GMP系统添加一个环境变量参数dateSwitchTimeInterval 1. insert.sh #!/bin/sh . ~/apphome/aic_export.sh #连接 ...

  5. shell脚本学习- 传递参数

    跟着RUNOOB网站的教程学习的笔记 我们可以在执行shell脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n代表一个数字,1为执行脚本的第一参数,2为执行脚本的第二个参数,以此类推... 实 ...

  6. Shell脚本之三 传递参数

    我们可以在执行 Shell 脚本时,向脚本传递参数,脚本内获取参数的格式为:$n.n 代表一个数字,1 为执行脚本的第一个参数,2 为执行脚本的第二个参数,以此类推-- 实例 向脚本传递三个参数,并分 ...

  7. shell脚本获取的参数

    $# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell脚本的第一个参数 $2 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表

  8. 向shell脚本中传入参数

    写一个 程序名为    test.sh    可带参数为 start 和 stop 执行  test.sh start执行  start 内容的代码 执行 test.sh stop 执行 stop 内 ...

  9. C++11的模板新特性-变长参数的模板

    这个特性很赞,直接给例子吧,假如我要设计一个类,CachedFetcher内部可能使用std::map也可能使用std::unordered_map,也可能是其它的map,怎么设计呢?没有C++11变 ...

随机推荐

  1. RunLoop总结:RunLoop的应用场景(五)

    今天要介绍的RunLoop应用场景感觉很酷炫,我们可能不常用到,但是对于做Crash 收集的 SDK可能会用得比较频繁吧.相比关于RunLoop 可以让应用起死回生,大家都听说过,可是怎么实现呢?今天 ...

  2. Android开发技巧——Camera拍照功能

    本篇是我对开发项目的拍照功能过程中,对Camera拍照使用的总结.由于camera2是在api level 21(5.0.1)才引入的,而Camera到6.0仍可使用,所以暂未考虑camera2. 文 ...

  3. Dynamics CRM 部署NLB后使用群集名称访问弹验证框验证不过的解决方法

    自上次部署NLB到现在已有段时间了,今天部署完后遇到了个问题,上次也遇到过但忘记了,本篇作为对该问题的一个记录,部署文档:https://blogs.msdn.microsoft.com/niran_ ...

  4. Programming In Scala笔记-第七章、Scala中的控制结构

    所谓的内建控制结构是指编程语言中可以使用的一些代码控制语法,如Scala中的if, while, for, try, match, 以及函数调用等.需要注意的是,Scala几乎所有的内建控制结构都会返 ...

  5. Android艺术开发探索——第二章:IPC机制(下)

    Android艺术开发探索--第二章:IPC机制(下) 我们继续来讲IPC机制,在本篇中你将会学习到 ContentProvider Socket Binder连接池 一.使用ContentProvi ...

  6. Android 如何监听输入法关闭事件

    假设有如下界面(输入法的上面的输入区域是用Dialog实现的) 要求当输入法关闭的时候,Dialog也一起关闭,这样用户就不需要返回两次了. 网上找了很多资料都没有很好的解决这个问题,输入法是第三方程 ...

  7. Android Multimedia框架总结(十一)CodeC部分之AwesomePlayer到OMX服务

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52623882 前言:上篇文< ...

  8. Markdown-----Markdown使用文档

    最近才接触Markdown,为了快速记忆,整理了这个文档,欢迎补充. Markdown和扩展Markdown简洁的语法 代码块高亮 图片链接和图片上传 LaTex数学公式 UML序列图和流程图 离线写 ...

  9. ToolBar与AppcompatAcitivity实现浸入式Statusbar效果

    toolbar是android sdk API21新增的组件,下面是谷歌官方的介绍文档: A standard toolbar for use within application content. ...

  10. JQuery之事件处理

    JQuery不支持捕获模型 冒泡模型解析 <body> <div> <input id="bntShow" type="button&quo ...