shell脚本练习(短路练习)
#!/bin/bash
#By Spinestars
#2013-11-11
#This is a lvsnap of auto-create
Help(){
echo "Usage:
---device eg:sd[b-z]
---partition_number eg:-
---partition_size -P \"1 2 3\" create partition
eg:lvsnap.sh -P \"sdc 1 4G\"
-p \"1 2\" create lv
-a \"1 2 3\"create all (interactive)
-h Usage
"
}
#**********************************************************
part(){
fdisk -l /dev/$ &&
read -p " if go on ,initialize the $1 partation [any press] or ctrl + c:"
dd if=/dev/zero of=/dev/$ bs= count=
sync
echo 'create partition:'
echo "n
p
$ +$
t
8e
w
p" | fdisk /dev/$1
sync
sleep
partx -a /dev/$$ /dev/$
echo "$1$2 has been created successfully!The size is $3!"
}
#**********************************************************
pv(){
ifpv=`pvdisplay | grep "/dev/$1$2"`
echo 'create pv...'
[ -z "$ifpv" ] &&
pvcreate /dev/$$ ||
echo "Warnning:PV:$1$2 is existed!" && read -p " if go on [any press] or ctrl + c:"
}
#**********************************************************
vg(){
read -p 'create vg(plesae input vgname):' vgname
ifvg=`vgdisplay | grep "$vgname"`
[ -z "$ifvg" ] &&
vgcreate $vgname /dev/$$ ||
echo "Warnning:$vgname is existed!" && read -p "if go on [any press] or ctrl + c:"
}
#**********************************************************
lv(){
read -p 'create lv(please input lvname):' lvname
read -p 'create lv(please input lvsize):' lvsize
iflv=`lvdisplay | grep "/dev/$vgname/$lvname"`
[ -z "$iflv" ] &&
lvcreate -n $lvname -L $lvsize $vgname &&
echo "initialize /dev/mapper/$vgname-$lvname:" &&
mke2fs -t ext4 /dev/mapper/$vgname-$lvname &&
sleep ||
echo "Warnning:$lvname is existed!" &&
read -p "if go on [any press] or ctrl + c:"
read -p "mount /dev/mapper/$vgname-$lvname(please input mount point and if point is not exist ,then create it):" point
ls / | grep "^`basename $point`$" &&
mount | grep "$lvname" &&
echo "$point is existed and is mounted!" ||
{
mkdir -p $point &&
mount -t ext4 /dev/mapper/$vgname-$lvname $point &&
[ $? -eq ] && echo "$vgname-$lvname is mounted successfully!"
}
}
#********************************************************
#while [ ];do
# [ $lvsnapsize -le $lvsize ] ||
# echo "Warnning:$lvsnapsize is too larger!" &&
# read -p 'Retry:input lvsnapsize:' lvsnapsize
#done
#********************************************************
lvsnap(){
read -p 'create lvsnap(please input lvsnapname):' lvsnapname
read -p 'create lvsnap(please input lvsnapsize):' lvsnapsize &&
iflv=`lvdisplay | grep "/dev/$vgname/$lvsnapname"`
[ -z "$iflv" ] &&
lvcreate -s -p r -n $lvsnapname -L $lvsnapsize /dev/mapper/$vgname-$lvname ||
echo "Warnning:$lvsnapname is existed!" && read -p "if go on [any press] or ctrl + c:"
read -p "mount /dev/mapper/$vgname-$lvsnapname(please input mount point and if point is not exist ,then create it):" snappoint
ls / | grep "^`basename $snappoint`$" &&
mount | grep "$lvsnapname" &&
echo "$snappoint is existed and is mounted!" ||
{
mkdir -p $snappoint &&
mount -t ext4 /dev/mapper/$vgname-$lvsnapname $snappoint &&
[ $? -eq ] && echo "$vgname-$lvsnapname is mounted successfully!"
}
}
#*********************************************************
tar2bak(){
bakfile_name="/backup/$lvname/$lvname-`date +%F_%T`.tar.xz"
echo $bakfile_name
mkdir "${bakfile_name%/*}"
echo "tar $snappoint to ${bakfile_name%/*}:"
tar Jcf $bakfile_name $snappoint
[ -f $bakfile_name ] &&
echo "$bakfile_name has been created successfully!"
sleep
echo "remove $lvsnapname:"
umount $snappoint
lvremove /dev/mapper/$vgname-$lvsnapname
} while getopts P:p:ha: select
do
case $select in
P)
read -p 'partition automation to create[yes|no]:' Partif
[ $Partif = 'yes' ] && part $OPTARG
;;
p)
pv $OPTARG
vg
lv
lvsnap
tar2bak && echo "backup success!" &&
find ${bakfile_name%/*} -amin -1 -type f -ls
;;
a)
read -p 'partition automation to create[yes|no]:' Partif
[ $Partif = 'yes' ] && part $OPTARG
read -p 'Pv automation to create[yes|no]:' Pvif
[ $Pvif = 'yes' ] && pv
read -p 'Vg automation to create[yes|no]:' Vgif
[ $Vgif = 'yes' ] && vg
read -p 'Lv automation to create[yes|no]:' Lvif
[ $Lvif = 'yes' ] && lv
read -p 'Lvsnap automation to create[yes|no]:' Lvsnapif
[ $Lvsnapif = 'yes' ] && lvsnap
read -p 'bak is baginning [yes|no]:' tar2bakif
[ $tar2bakif = 'yes' ] && tar2bak && echo "backup success!" &&
find ${bakfile_name%/*} -amin -1 -type f -ls
;;
*)
Help
;;
esac
done
shell脚本练习(短路练习)的更多相关文章
- Linux shell脚本编程(二)
Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...
- shell && 和 || 的短路使用
shell && 和 || 的短路使用 && 和 || 在 shell 中分别表示 and 和 or,和其它语言类似,这两个操作有短路效应.也就是说,当判断式已经确定时 ...
- shell脚本编程之for语句、if语句使用介绍
介绍了shell脚本编程之for语句.if语句的使用方法. 上部: 面向过程: 顺序执行 选择执行: if, case 循环执行: for, while, until 一.for语句 格式: ...
- [转载]关于shell脚本的基本语法
关于shell脚本的基本语法 整理于:2014-03-31,何俭飞,mymladdr@sina.com 一.执行 1.shell脚本如果要被执行,一般地必须要有执行权限"x"(除了 ...
- Shell脚本编写
1.什么是Shell脚本 Shell脚本是利用 shell 的功能所写的一个程序 program,这个程序是使用纯文本文件,将一些 shell 的语法与指令(含外部指令)写在里面, 搭配正则表达式.管 ...
- shell脚本编写实例
实际案例 1.判断接收参数个数大于1 [ $# -lt 1 ] && echo "至少需要一个参数" && { echo "我要退出了.. ...
- Linux基础篇–shell脚本编程基础
本章内容概要 编程基础 脚本基本格式 变量 运算 条件测试 配置用户环境 7.1 编程基础程序:指令+数据程序编程风格: 过程式:以指令为中心,数据服务于指令 对象式:以数据为中心 ...
- LINUX SHELL脚本攻略笔记[速查]
Linux Shell脚本攻略笔记[速查] 资源 shell script run shell script echo printf 环境变量和变量 pgrep shell数学运算 命令状态 文件描述 ...
- shell脚本-基础
shell脚本-基础 编程基础 程序是指令+ 数据 程序编程风格: 过程式:以指令为中心,数据服务于指令 对象式:以数据为中心,指令服务于数据 shell 程序提供了编程能力,解释执行. 计算运行二进 ...
随机推荐
- meta 属性
几乎所有的网页里,我们可以看到类似下面这段的html代码:<head><meta http-equiv="content-Type" content=" ...
- Xcode中C、C++、Object-C3种语言的混编
转自: http://hi.baidu.com/onejw/item/f34390c997cdc226a1b50ae http://www.cocoachina.com/ask/questions/s ...
- The Contiki build system 编译系统
The Contiki build system======================== The Contiki build system is designed to make it eas ...
- ORA-19502: write error on file "", blockno (blocksize=)/linux下磁盘空间满了解决办法--Virtualbox
今天,在测试环境启动数据库时,报错: SQL> startup; ORACLE instance started. Total System Global Area 285212672 byt ...
- OpenWRT 编译 error GNU libiconv not in use but included iconv.h is from...
OpenWRT 编译 error GNU libiconv not in use but included iconv.h is from... 编译的时候碰到一个常见的错误,但是却在一个陌生的地方爆 ...
- 方案:在Eclipse IDE 中搭建Python开发环境
Eclipse是一款功能强大的IDE,Python是一种功能强大的计算机语言,但是Python的IDE环境确实很缺乏,如果在强大的Eclipse中添加Python开发环境,那样就很完美了. 在这里,我 ...
- Reverse Linked List II 解答
Question Reverse a linked list from position m to n. Do it in-place and in one-pass. For example:Giv ...
- nyoj 36 最长公共子序列
描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列. tip:最长公共子序列也称作最长公共子串(不要求连续),英文缩写为LCS(Longest Common Subseque ...
- 《招聘一个靠谱的iOS》面试题参考答案(上)
说明:面试题来源是微博@我就叫Sunny怎么了的这篇博文:<招聘一个靠谱的 iOS>,其中共55题,除第一题为纠错题外,其他54道均为简答题. 博文中给出了高质量的面试题,但是未给出答案, ...
- Android——SQLite实现面向对象CRUD
android中SQLite的使用,事实上倒也不难.可是与JDBC操作数据库相比,这个还是有点不顺手,并且我好久没写底层的封装了,使用SSM框架这些都不须要考虑......好了,废话不多说.以下直接建 ...