`
qindongliang1922
  • 浏览: 2172087 次
  • 性别: Icon_minigender_1
  • 来自: 北京
博客专栏
7265517b-f87e-3137-b62c-5c6e30e26109
证道Lucene4
浏览量:117105
097be4a0-491e-39c0-89ff-3456fadf8262
证道Hadoop
浏览量:125444
41c37529-f6d8-32e4-8563-3b42b2712a50
证道shell编程
浏览量:59547
43832365-bc15-3f5d-b3cd-c9161722a70c
ELK修真
浏览量:71031
社区版块
存档分类
最新评论

跟散仙学shell编程(二)

阅读更多
散仙本篇主要讲在shell里面的流程控制,流程控制是所有的编程语言里面必不可少的一部分,通过流程控制,可以使我们的程序更加灵活。



下面我们来看看如何在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语句,来使我们的流程更加清晰。






















































0
0
分享到:
评论
1 楼 iDeputy 2014-10-16  
下面看下if-then-else语句的使用:
下面的例子不对,没有else

相关推荐

    跟老男孩学Linux运维:Shell编程实战

    《跟老男孩学Linux运维:Shell编程实战》第二部分为第5章~第8章,着重讲解变量的多种数值运算、条件测试与比较、if条件判断语句、Shell函数等相关的知识。《跟老男孩学Linux运维:Shell编程实战》第三部分为第9章~第...

    跟老男孩学Linux运维:Shell编程实战 PDF

    跟老男孩学Linux运维:Shell编程实战 PDF跟老男孩学Linux运维:Shell编程实战 PDF

    跟老男孩学Linux运维:Shell编程实战.pdf 高清 带书签

    资深运维架构实战专家及教育培训界*专家十多年的运维实战经验总结,全面系统地讲解运维工作中Shell编程所需的知识点和Shell编程的各种企业级案例。

    shell编程入门经典--LINUX与UNIX Shell编程指南 (中文pdf版)

    《LINUX与UNIX Shell编程指南》是一本专为初学者设计的shell编程教程,它深入浅出地介绍了在Linux和UNIX系统中如何使用Shell进行高效自动化任务处理。Shell编程是Linux和UNIX系统中的核心技术,它允许用户通过命令行...

    shell编程学习资料

    Shell编程是Linux/Unix系统中不可或缺的一部分,它是一种命令行解释器,允许用户与操作系统进行交互,执行系统命令,以及编写脚本自动化任务。在本文中,我们将深入探讨Shell编程的基础知识,包括基本命令、变量、...

    shell编程指南shell编程指南shell编程指南shell编程指南

    Shell编程是一种在Unix/Linux操作系统环境下进行系统管理与自动化任务的强大工具。它允许用户通过命令行接口执行各种操作,包括文件管理、程序控制、系统管理任务等。本指南将深入探讨Shell编程的基础知识、常用命令...

    Windows Shell 编程.pdf

    Windows Shell 编程.pdf 看过一些对windows 外壳的扩展程序,在使用上一般都是直接利用windows的外壳API做一些工作,因为外壳操作需要一些比较专业的知识,因此,大部分编程人员特别是使用集成编程环境的程序人员对...

    shell编程入门教程+shell脚本专家指南+UNIX.shell编程24小时教程.rar

    《shell编程入门教程》、《shell脚本专家指南》以及《UNIX.shell编程24小时教程》会提供详尽的实例和练习,帮助你巩固所学并深化理解。 总之,Shell编程是Linux/Unix环境中不可或缺的技能,它能够提高工作效率,...

    shell编程题目练习

    shell编程题目练习,练习基本的shell编程,学习脚本语言,提高效率

    Shell编程高级进阶系列视频.zip

    13Linux下Shell编程之While case演练 14Linux下Shell编程之While case演练 15Shell编程之函数及脚本案例讲解 16Shell编程之函数及脚本案例讲解 17Linux下Shell编程FIND、SED命令实战 18Linux下Shell编程FIND、SED...

    跟老男孩学Linux运维:Shell编程实战 完整版 pdf

    跟老男孩学Linux运维:Shell编程实战 完整版 pdf

    Linux与UNIX Shell编程指南.pdf

    "Linux与UNIX Shell编程指南" Linux与UNIX Shell编程指南是计算机科学领域中一本经典的指南手册,旨在帮助读者快速掌握Linux与UNIX操作系统下的shell编程技术。下面是从该书中生成的相关知识点: 1. Shell概述 ...

    Windows Shell 编程指南与实例

    《Windows Shell 编程指南与实例》是一本深入探讨Windows操作系统壳层编程技术的专业书籍。在Windows系统中,Shell指的是用户界面,它为用户提供与操作系统交互的环境,包括桌面、开始菜单、快捷方式等。Shell编程则...

    绝版经典 LINUX与UNIX SHELL编程指南 PDF 高清版 [16.3M]

    学完本书后,你将成为一名shell编程高手。 内容简介 本书共分五部分 ,详细介绍了SHELL编程技巧,各种UNIX命令及语法,还涉及了UNIX的文字处理以及少量的系统管理问题。本书内容全面,文字简洁流畅,适合SHELL编程...

    UNIX命令及SHELL编程

    这是一套完整的Unix培训教材,包括Unix常用命令及SHELL编程基础与高级技巧,PDF格式,共30个文件。另有2个Word文档。包内文件清单如下: 01_Shell-文件安全与权限.PDF 02_Shell-使用find和xargs.PDF 03_Shell-...

    Shell编程中文手册.pdf

    Shell 编程中文手册 本手册涵盖了 Shell 编程的基础知识,包括 Shell 概述、Shell 解析器、Shell 脚本入门、Shell 中的变量等。 Shell 概述 Shell 是一种命令行接口,允许用户与操作系统进行交互。学习 Shell ...

    Unix Shell Shell编程

    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编程个人...

    shell demo及编程pdf

    **Shell编程介绍** Shell编程是Linux/Unix操作系统中的一种脚本语言,用于自动化日常任务,交互式地控制操作系统,以及实现系统级别的程序间交互。它提供了命令行接口(CLI)来执行各种系统命令,使用户能够高效地...

Global site tag (gtag.js) - Google Analytics