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. Redis之(二)数据类型及存储结构

    Redis支持五中数据类型:String(字符串),Hash(哈希),List(列表),Set(集合)及zset(sortedset:有序集合). Redis定义了丰富的原语命令,可以直接与Redis ...

  2. An internal error occurred during: "Retrieving archetypes:". GC overhead limit exceeded

    An internal error occurred during: "Retrieving archetypes:".GC overhead limit exceeded 异常, ...

  3. Swift基础之两指拉动图片变大变小

    我们在使用APP的时候,有时会发现有些图片可以通过两指进行放大.缩小,今天就实现这样的一种效果,比较简单,不喜勿喷.... var imageVi:UIImageView! = nil    var ...

  4. python 列表解析与map和filter函数

    不知哪儿看到一个说法,大概是当map的函数参数可以直接引用一个已有的函数变量时(比如内建函数int,str之类的),用map更优美些,否则还是用列表解析更直观和快速. 我同意此说法. 昨天在写一个函数 ...

  5. SpriteKit:检测当新场景显示以后

    Detecting When a New Scene Is Presented Sprite Kit在SKScene类中提供2个可以重载的方法用来检测当一个场景过渡出去或过渡进来的时候. 第一个方法是 ...

  6. Android 系统自动重启Bug(高通平台)

    点击打开链接 最近客户反馈了一个Bug,我们的系统用着用着会自动重启,尤其是在拨号的时候极容易死机或者进入下载模式.根据老大和高通的支持得到了一个解决方案. 在Android系统中,有这么一个文件夹: ...

  7. 16 Content Provider总结

    第16天 Content Provider 一, 什么是Content Provider? 内容提供者 Android四大主件之一 :短信记录 通讯录 联系人 自定义 >Content Prov ...

  8. iOS开发中的零碎知识点笔记 韩俊强的博客

    每日更新关注:http://weibo.com/hanjunqiang  新浪微博 1.关联 objc_setAssociatedObject关联是指把两个对象相互关联起来,使得其中的一个对象作为另外 ...

  9. Git管理工具对比(GitBash、EGit、SourceTree)

    Git管理工具对比(GitBash.EGit.SourceTree) GitBash是采用命令行的方式对版本进行管理,功能最为灵活强大,但是由于需要手动输入希望修改的文件名,所以相对繁琐. EGit是 ...

  10. 《java入门第一季》之HashSet存储自定义对象问题以及注意事项

    上一篇http://blog.csdn.net/qq_32059827/article/details/51578158 写到存储字符串类型的时候出现了无序,而且这个无序不是随机那种无序,它是有一定存 ...