- 浏览: 2149479 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (682)
- 软件思想 (7)
- Lucene(修真篇) (17)
- Lucene(仙界篇) (20)
- Lucene(神界篇) (11)
- Solr (48)
- Hadoop (77)
- Spark (38)
- Hbase (26)
- Hive (19)
- Pig (25)
- ELK (64)
- Zookeeper (12)
- JAVA (119)
- Linux (59)
- 多线程 (8)
- Nutch (5)
- JAVA EE (21)
- Oracle (7)
- Python (32)
- Xml (5)
- Gson (1)
- Cygwin (1)
- JavaScript (4)
- MySQL (9)
- Lucene/Solr(转) (5)
- 缓存 (2)
- Github/Git (1)
- 开源爬虫 (1)
- Hadoop运维 (7)
- shell命令 (9)
- 生活感悟 (42)
- shell编程 (23)
- Scala (11)
- MongoDB (3)
- docker (2)
- Nodejs (3)
- Neo4j (5)
- storm (3)
- opencv (1)
最新评论
-
qindongliang1922:
粟谷_sugu 写道不太理解“分词字段存储docvalue是没 ...
浅谈Lucene中的DocValues -
粟谷_sugu:
不太理解“分词字段存储docvalue是没有意义的”,这句话, ...
浅谈Lucene中的DocValues -
yin_bp:
高性能elasticsearch ORM开发库使用文档http ...
为什么说Elasticsearch搜索是近实时的? -
hackWang:
请问博主,有用solr做电商的搜索项目?
Solr中Group和Facet的用法 -
章司nana:
遇到的问题同楼上 为什么会返回null
Lucene4.3开发之第八步之渡劫初期(八)
散仙本篇主要讲在shell里面的流程控制,流程控制是所有的编程语言里面必不可少的一部分,通过流程控制,可以使我们的程序更加灵活。
下面我们来看看如何在shell里面使用if else流程控制语句,shell里面的流程控制语句比较特殊的其他的编程语言里,比如JAVA,都是通过一个boolean的值,来判断是否通过某个流程,在shell里面,却是通过shell执行命令的状态码来识别的,返回为0的状态码,代表为true。
if then还可以不用换行写,但需要加个分号如下:
下面看下if-then-else语句的使用:
下面看下多重if-else的使用:
下面说下test命令,test命令提供了在if-then语句中测试不同条件的途径,如果test中的条件成立,test命令的就会退出状态码为0,如果不成立,就会返回状态码1,
命令格式:
if test condition
then
commands
fi
这个等同于下面的写法在bash shell里面的写法:
if [ condition ]
then
commands
fi
方括号定义了test命令用到的条件,注意括号两侧都需要加一个空格,否则会报错。
test命令,可以判断3类条件:
数值比较;
字符串比较;
文件比较;
数值比较:
n1 -eq n2 判断n1是否和n2相等
n1 -ge n2 判断是否大于或等于n2
n1 -gt n2 检查n1是否大于n2
n1 -le n2 检查n1是否小于或等于n2
n1 -lt n2 检查n1是否小于n2
n1 -ne n2 检查n1是否不等于n2
下面给个字符串比较的例子:
注意数值相等比较用=号,方括号左右都必须有一个空格
字符串比较的一些用法:
(注意在方括号内的>,<符号,比较字符串需要转义\>)
str1 = str2 字符串相等比较
str1 != str2 字符串不相等比较
str1 < str2 检查str1是否小于str2(按字典顺序)
str1 > str2 检查str1是否大于str2(按字典顺序)
-n str 检查字符串str的长度是否非0
-z str 检查字符串str的长度是否为0
最后看下文件比较
-d file 检查file是否存在并是否为一个目录
-e file 检查file是否存在
-f file 检查file是否存在并是一个文件
-r file 检查file是否存在并可读
-s file 检查file是否存在并是否非空
-w file 检查file是否存在并可写
-x file 检查file是否存在并可执行
-O file 检查file是否存在并属于当前用户所有
-G file 检查file是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧
比较新旧文件的命令:
多个条件组合测试:
&&两个条件同时满足
||满足其中一个即可
上面的表达式比较繁琐,shell里面特意提供了针对数学运算的((exprssion))双圆括号的命令和针对字符串的双方括号的命令:
双方括号提供了,更高级的字符串操作命令,利用了正则的用法:
注意在==号右边的正则式不能用双引号括起来
最后再来看下多重if-else的替代命令case命令的用法:
语法格式:
case var in
pattern1 | pattern2 ) commands1::
pattern3) commands2::
*) default commands::
esac
例子如下:
注意最后的结束符,是由两个挨着的分号构成的,通过上面的例子,我们就会发现shell里面的case语句非常强大和灵活,尽可能的使用case语句,来使我们的流程更加清晰。
下面我们来看看如何在shell里面使用if else流程控制语句,shell里面的流程控制语句比较特殊的其他的编程语言里,比如JAVA,都是通过一个boolean的值,来判断是否通过某个流程,在shell里面,却是通过shell执行命令的状态码来识别的,返回为0的状态码,代表为true。
[root@h1 test]# cat test.sh if date then echo "success!" fi [root@h1 test]# sh test.sh 2014年 08月 09日 星期六 04:13:47 CST success! [root@h1 test]#
if then还可以不用换行写,但需要加个分号如下:
[root@h1 test]# cat test.sh if date ;then echo "success!" fi [root@h1 test]# sh test.sh 2014年 08月 09日 星期六 04:15:42 CST success! [root@h1 test]#
下面看下if-then-else语句的使用:
[root@h1 test]# cat test.sh if date ;then echo "success!" fi [root@h1 test]# sh test.sh 2014年 08月 09日 星期六 04:15:42 CST success! [root@h1 test]#
下面看下多重if-else的使用:
[root@h1 test]# cat c.sh if (( 3 < 2 )) ; then echo "3<2" elif (( 4 < 3 )); then echo "4<3" elif (( 90 < 40 )); then echo "90<100" else echo "all is error!" fi [root@h1 test]# sh c.sh all is error! [root@h1 test]#
下面说下test命令,test命令提供了在if-then语句中测试不同条件的途径,如果test中的条件成立,test命令的就会退出状态码为0,如果不成立,就会返回状态码1,
命令格式:
if test condition
then
commands
fi
这个等同于下面的写法在bash shell里面的写法:
if [ condition ]
then
commands
fi
方括号定义了test命令用到的条件,注意括号两侧都需要加一个空格,否则会报错。
test命令,可以判断3类条件:
数值比较;
字符串比较;
文件比较;
数值比较:
n1 -eq n2 判断n1是否和n2相等
n1 -ge n2 判断是否大于或等于n2
n1 -gt n2 检查n1是否大于n2
n1 -le n2 检查n1是否小于或等于n2
n1 -lt n2 检查n1是否小于n2
n1 -ne n2 检查n1是否不等于n2
[root@h1 test]# cat d.sh if [ 10 -eq 12 ] ; then echo "10==12" else echo "10!=12" fi if [ 3 -ge 1 ] ; then echo "3 > 1" else echo "3 < 1" fi [root@h1 test]# sh d.sh 10!=12 3 > 1 [root@h1 test]#
下面给个字符串比较的例子:
[root@h1 test]# cat e.sh if [ "abc" = "abc" ];then echo "abc=abc" else echo "abc!=abc" fi echo "=======================" if [ 'abc' = "abc" ]; then echo "abc=abc" fi #数值类型的等号比较 if [ 6 = 7 ] ;then echo "6=7" else echo "6!=7" fi [root@h1 test]# sh e.sh abc=abc ======================= abc=abc 6!=7 [root@h1 test]#
注意数值相等比较用=号,方括号左右都必须有一个空格
字符串比较的一些用法:
(注意在方括号内的>,<符号,比较字符串需要转义\>)
str1 = str2 字符串相等比较
str1 != str2 字符串不相等比较
str1 < str2 检查str1是否小于str2(按字典顺序)
str1 > str2 检查str1是否大于str2(按字典顺序)
-n str 检查字符串str的长度是否非0
-z str 检查字符串str的长度是否为0
[root@h1 test]# cat aa.sh str="china" str2="" if [ -n $str ] ; then echo "str is not empty" else echo "str is empty" fi if [ -z $str2 ] ; then echo "str2 is empty" else echo "str2 is not empty" fi time=`date` echo "当前时间: $time" [root@h1 test]# sh aa.sh str is not empty str2 is empty 当前时间: 2014年 08月 09日 星期六 04:58:43 CST [root@h1 test]#
最后看下文件比较
-d file 检查file是否存在并是否为一个目录
-e file 检查file是否存在
-f file 检查file是否存在并是一个文件
-r file 检查file是否存在并可读
-s file 检查file是否存在并是否非空
-w file 检查file是否存在并可写
-x file 检查file是否存在并可执行
-O file 检查file是否存在并属于当前用户所有
-G file 检查file是否存在并且默认组与当前用户相同
file1 -nt file2 检查file1是否比file2新
file1 -ot file2 检查file1是否比file2旧
[root@h1 test]# cat bb.sh #测试是否为目录 if [ -d $HOME ] ; then echo "it is a dir" cd $HOME ls -la else echo "it is not a dir" fi echo "==========================" #测试是否为文件 if [ -f /etc/profile ] ; then echo "it is a file" else echo "it is not a file" fi [root@h1 test]# sh bb.sh it is a dir 总用量 329652 dr-xr-x---. 13 root root 4096 8月 9 05:07 . dr-xr-xr-x. 22 root root 4096 8月 8 19:34 .. -rw-r--r-- 1 root root 143775368 7月 28 19:30 abc1.txt -rw-------. 1 root root 1087 6月 13 19:06 anaconda-ks.cfg -rw-r--r-- 1 root root 65 8月 8 04:11 a.sh -rw-------. 1 root root 10267 8月 9 04:22 .bash_history -rw-r--r--. 1 root root 18 5月 20 2009 .bash_logout -rw-r--r--. 1 root root 176 5月 20 2009 .bash_profile -rw-r--r--. 1 root root 119 6月 16 21:12 .bashrc -rw-r--r-- 1 root root 75 8月 8 05:07 bc.sh -rw-r--r-- 1 root root 90 8月 8 04:14 b.sh -rw-r--r-- 1 root root 141 8月 8 05:12 cc.sh -rw-r--r-- 1 root root 52 7月 31 21:29 count2.txt -rw-r--r-- 1 root root 52 7月 31 19:46 count.txt -rw-r--r-- 1 root root 127 8月 8 04:20 c.sh -rw-r--r--. 1 root root 100 9月 23 2004 .cshrc -rw-r--r--. 1 root root 96183833 6月 9 17:27 hadoop-2.2.0.tar.gz -rw-r--r-- 1 root root 1 7月 31 21:25 hh.txt drwxr-xr-x 3 root root 4096 7月 29 04:47 hivesrc -rw-r--r--. 1 root root 2111 6月 16 13:10 initserver.sh -rw-r--r--. 1 root root 7995 6月 13 19:06 install.log -rw-r--r--. 1 root root 3384 6月 13 19:06 install.log.syslog drwxr-xr-x 2 root root 4096 7月 31 21:19 intest lrwxrwxrwx 1 root root 12 7月 31 21:45 jdk -> jdk1.7.0_25/ drwxr-xr-x. 8 search 143 4096 6月 6 2013 jdk1.7.0_25 -rwx------. 1 root root 96316511 11月 20 2013 jdk-7u25-linux-x64.gz drwxr-xr-x 3 root root 4096 7月 31 21:33 li drwxr-xr-x 3 root root 4096 7月 9 04:08 lo -rw-r--r-- 1 root root 25 8月 8 04:20 log.140808 drwxr-xr-x 3 root root 4096 7月 9 04:08 login drwxr-xr-x 3 root root 4096 7月 29 04:11 .m2 -rw------- 1 root root 727 7月 29 01:44 .mysql_history -rw-r--r-- 1 root root 1048 6月 19 03:31 setlimit.sh drwx------. 2 root root 4096 6月 16 21:00 .ssh -rw-r--r--. 1 root root 129 12月 4 2004 .tcshrc drwxr-xr-x 2 root root 4096 8月 9 05:07 test drwxr-sr-x 2 root abc 4096 8月 6 01:53 testidr -rwxr--r-- 1 root root 46 8月 8 03:43 test.sh drwxr-xr-x 3 root root 4096 6月 20 02:51 tsethadoop -rw-r--r-- 1 root root 87 8月 8 05:29 t.sh -rw------- 1 root root 5191 8月 9 05:07 .viminfo -rw-r--r-- 1 root root 1110849 4月 7 17:51 歌曲.mp3 ========================== it is a file [root@h1 test]#
比较新旧文件的命令:
[root@h1 test]# ll 总用量 36 -rw-r--r-- 1 root root 228 8月 9 04:58 aa.sh -rw-r--r-- 1 root root 23 8月 5 01:44 a.sh -rw-r--r-- 1 root root 275 8月 9 05:07 bb.sh -rw-r--r-- 1 root root 54 8月 9 04:19 b.sh -rw-r--r-- 1 root root 90 8月 9 05:11 cc.sh -rw-r--r-- 1 root root 145 8月 9 04:25 c.sh -rw-r--r-- 1 root root 128 8月 9 04:37 d.sh -rw-r--r-- 1 root root 236 8月 9 04:43 e.sh -rw-r--r-- 1 root root 38 8月 9 04:15 test.sh [root@h1 test]# cat cc.sh if [ ./a.sh -nt ./b.sh ] ; then echo "a.sh is new " else echo "b.sh is new" fi; [root@h1 test]# sh cc.sh b.sh is new [root@h1 test]#
多个条件组合测试:
&&两个条件同时满足
||满足其中一个即可
[root@h1 test]# cat dd.sh if [ 3 \> 2 ] && [ 4 \> 5 ] ; then echo "3>2 and 4>5" else echo "condition is not fit" fi echo "==============================================" if [ 3 \> 2 ] && [ 9 \> 5 ] ; then echo "3>2 and 9>5" else echo "condition is not fit" fi [root@h1 test]# sh dd.sh condition is not fit ============================================== 3>2 and 9>5 [root@h1 test]#
上面的表达式比较繁琐,shell里面特意提供了针对数学运算的((exprssion))双圆括号的命令和针对字符串的双方括号的命令:
[root@h1 test]# cat z.sh if (( 4 > 6 )) ; then echo "4 > 6" else echo " 4 > 6" fi [root@h1 test]# sh z.sh 4 > 6 [root@h1 test]#
双方括号提供了,更高级的字符串操作命令,利用了正则的用法:
[root@h1 test]# cat h.sh if [[ "sanxian" == s* ]] ; then echo "begin with s" else echo "begin is not s" fi [root@h1 test]# sh h.sh begin with s [root@h1 test]#
注意在==号右边的正则式不能用双引号括起来
最后再来看下多重if-else的替代命令case命令的用法:
语法格式:
case var in
pattern1 | pattern2 ) commands1::
pattern3) commands2::
*) default commands::
esac
例子如下:
[root@h1 test]# cat case.sh case "1" in "1" | "11") echo "execute this 1" ;; "2") echo "execute this 2" ;; *) echo "this is all default" ;; esac [root@h1 test]# sh case.sh execute this 1 [root@h1 test]#
注意最后的结束符,是由两个挨着的分号构成的,通过上面的例子,我们就会发现shell里面的case语句非常强大和灵活,尽可能的使用case语句,来使我们的流程更加清晰。
发表评论
-
备忘几个有用的shell脚本
2015-04-07 21:59 1462(1)在shell下,操作hadoop目录,批量命名或删除, ... -
shell脚本杂记(五)
2014-09-22 19:48 19881,找出多个文件所占用的磁盘空间 du f1 f2 [roo ... -
shell脚本杂记(四)
2014-09-19 20:03 16641,wget是一个用于文件 ... -
shell自动化部署批量建立用户和批量SSH配置
2014-09-18 18:29 3566在linux下面,shell脚本熟练使用,有时候可以帮助我们解 ... -
shell自动化部署神器expect
2014-09-17 21:08 4637Shell可以实现简单的控制流功能,如:循环、判断等。但是对于 ... -
shell脚本杂记(三)
2014-09-16 19:58 15711,我们先来熟悉下正则表达式一些字符含义的基础 ^行起始标记 ... -
shell脚本杂记(二)
2014-09-15 21:25 19841,求两个文件的交集comm a.txt b.txt -1 - ... -
shell脚本杂记(一)
2014-09-12 21:50 19941,登陆系统时,$代表 ... -
跟散仙学shell编程(十五)
2014-08-27 21:24 1796上篇文章,散仙简单介绍了,如何在Linux下以shell脚本的 ... -
跟散仙学shell编程(十四)
2014-08-26 20:59 1900上篇散仙简单介绍了linu ... -
跟散仙学shell编程(十三)
2014-08-25 20:02 1274上篇散仙主要了讲述了gawk进阶的一些用法,本篇我们来了解下载 ... -
跟散仙学shell编程(十二)
2014-08-22 21:27 1547上篇文章散仙写了关于sed进阶,本篇我们来看下在linux中另 ... -
跟散仙学shell编程(十一)
2014-08-21 21:45 1597上篇散仙写了关于shell里面正则的基础知识,本篇我们来特意学 ... -
跟散仙学shell编程(十)
2014-08-20 21:22 1290上篇文章,散仙简单介绍了在linux中,使用sed和gawk编 ... -
跟散仙学shell编程(九)
2014-08-19 21:49 1670上篇散仙说了如何在linux里面构建更好的交互式shell,本 ... -
跟散仙学shell编程(八)
2014-08-18 21:50 1596上篇散仙说了linux里面函数的使用,本篇我们来看下如何在sh ... -
跟散仙学shell编程(七)
2014-08-15 21:46 1765上篇散仙在文章里描述了如何处理linux信号和脚本控制,本节我 ... -
跟散仙学shell编程(六)
2014-08-14 21:53 2045上篇介绍了linux里面的处理数据的方式,本篇散仙来说下,如何 ... -
跟散仙学shell编程(五)
2014-08-13 21:41 1630上一篇写的是处理用户输入,本篇散仙要写的是linux里面的标准 ... -
跟散仙学shell编程(四)
2014-08-12 21:46 1495上篇介绍了for循环命令 ...
相关推荐
shell编程指南shell编程指南shell编程指南shell编程指南shell编程指南shell编程指南shell编程指南shell编程指南
《跟老男孩学Linux运维:Shell编程实战》第二部分为第5章~第8章,着重讲解变量的多种数值运算、条件测试与比较、if条件判断语句、Shell函数等相关的知识。《跟老男孩学Linux运维:Shell编程实战》第三部分为第9章~第...
shell编程学习资料,常见的shell编程指导,sed,awk。
跟老男孩学Linux运维:Shell编程实战 PDF跟老男孩学Linux运维:Shell编程实战 PDF
资深运维架构实战专家及教育培训界*专家十多年的运维实战经验总结,全面系统地讲解运维工作中Shell编程所需的知识点和Shell编程的各种企业级案例。
初学者入门教程:Shell编程概述(二) shell编程-shell编程-分支语句(1) shell编程-shell编程-分支语句(2) shell编程-shell编程-循环语句(1) shell编程-shell编程-循环语句(2) Linux的shell...
shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程shell 编程...
shell编程题目练习,练习基本的shell编程,学习脚本语言,提高效率
shell编程shell编程shell编程shell编程
13Linux下Shell编程之While case演练 14Linux下Shell编程之While case演练 15Shell编程之函数及脚本案例讲解 16Shell编程之函数及脚本案例讲解 17Linux下Shell编程FIND、SED命令实战 18Linux下Shell编程FIND、SED...
跟老男孩学Linux运维:Shell编程实战 完整版 pdf
LINUX与UNIX SHELL编程指南
学完本书后,你将成为一名shell编程高手。 内容简介 本书共分五部分 ,详细介绍了SHELL编程技巧,各种UNIX命令及语法,还涉及了UNIX的文字处理以及少量的系统管理问题。本书内容全面,文字简洁流畅,适合SHELL编程...
shell 编程学习 详细的介绍了shell的东西 非常不错
这是一套完整的Unix培训教材,包括Unix常用命令及SHELL编程基础与高级技巧,PDF格式,共30个文件。另有2个Word文档。包内文件清单如下: 01_Shell-文件安全与权限.PDF 02_Shell-使用find和xargs.PDF 03_Shell-...
shell编程入门经典--LINUX与UNIX Shell编程指南 (中文pdf版)
6本pdf及chm的shell 编程的书 6本pdf及chm的shell 编程的书 6本pdf及chm的shell 编程的书 6本pdf及chm的shell 编程的书 6本pdf及chm的shell 编程的书
Shell编程Shell编程Shell编程Shell编程Shell编程Shell编程Shell编程Shell编程Shell编程Shell编程Shell编程
shell编程入门教程+shell脚本专家指南+UNIX.shell编程24小时教程
经典Linux和unix的Shell编程,按照模板练习即可快速提高。