跟散仙学shell编程

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



下面我们来看看如何在shell里面使用if else流程控制语句,shell里面的流程控制语句比较特殊的其他的编程语言里,比如JAVA,都是通过一个boolean的值,来判断是否通过某个流程,在shell里面,却是通过shell执行命令的状态码来识别的,返回为0的状态码,代表为true。 


Java代码   收藏代码
  1. [root@h1 test]# cat test.sh   
  2.   
  3.   
  4. if date   
  5. then  
  6.   
  7.  echo "success!"  
  8.   
  9. fi  
  10. [root@h1 test]# sh test.sh   
  11. 2014年 08月 09日 星期六 04:13:47 CST  
  12. success!  
  13. [root@h1 test]#   


if then还可以不用换行写,但需要加个分号如下: 
Java代码   收藏代码
  1. [root@h1 test]# cat test.sh   
  2.   
  3.   
  4. if date ;then  
  5.   
  6.  echo "success!"  
  7.   
  8. fi  
  9. [root@h1 test]# sh test.sh   
  10. 2014年 08月 09日 星期六 04:15:42 CST  
  11. success!  
  12. [root@h1 test]#   


下面看下if-then-else语句的使用: 

Java代码   收藏代码
  1. [root@h1 test]# cat test.sh   
  2.   
  3.   
  4. if date ;then  
  5.   
  6.  echo "success!"  
  7.   
  8. fi  
  9. [root@h1 test]# sh test.sh   
  10. 2014年 08月 09日 星期六 04:15:42 CST  
  11. success!  
  12. [root@h1 test]#   


下面看下多重if-else的使用: 

Java代码   收藏代码
  1. [root@h1 test]# cat c.sh   
  2.   
  3. if (( 3 < 2  )) ; then  
  4.   
  5. echo "3<2"  
  6. elif (( 4 < 3  )); then  
  7. echo "4<3"  
  8.   
  9. elif (( 90 < 40 )); then  
  10. echo "90<100"  
  11.   
  12. else   
  13. echo "all is error!"  
  14.   
  15. fi  
  16.   
  17.   
  18. [root@h1 test]# sh c.sh   
  19. all is error!  
  20. [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 

Java代码   收藏代码
  1. [root@h1 test]# cat d.sh   
  2.   
  3.   
  4. if [ 10 -eq 12   ] ; then  
  5. echo "10==12"  
  6. else  
  7. echo "10!=12"  
  8. fi  
  9.   
  10.   
  11.   
  12. if [  3 -ge 1   ] ; then  
  13. echo "3 > 1"  
  14. else  
  15. echo "3 < 1"  
  16. fi  
  17.   
  18.   
  19. [root@h1 test]# sh d.sh   
  20. 10!=12  
  21. 3 > 1  
  22. [root@h1 test]#   


下面给个字符串比较的例子: 

Java代码   收藏代码
  1. [root@h1 test]# cat e.sh   
  2.   
  3.   
  4. if [  "abc" = "abc"   ];then  
  5. echo "abc=abc"  
  6. else  
  7. echo "abc!=abc"  
  8. fi  
  9. echo "======================="  
  10.   
  11. if [  'abc' = "abc"  ]; then  
  12. echo "abc=abc"  
  13. fi  
  14.   
  15.   
  16.   
  17. #数值类型的等号比较  
  18. if  [ 6 = 7 ] ;then  
  19. echo "6=7"  
  20. else   
  21. echo "6!=7"  
  22. fi  
  23.   
  24.   
  25.   
  26. [root@h1 test]# sh e.sh   
  27. abc=abc  
  28. =======================  
  29. abc=abc  
  30. 6!=7  
  31. [root@h1 test]#   

注意数值相等比较用=号,方括号左右都必须有一个空格 

字符串比较的一些用法: 
(注意在方括号内的>,<符号,比较字符串需要转义\>) 
str1 = str2 字符串相等比较 
str1 != str2 字符串不相等比较 
str1 < str2  检查str1是否小于str2(按字典顺序) 
str1 > str2  检查str1是否大于str2(按字典顺序) 
-n str   检查字符串str的长度是否非0 
-z str   检查字符串str的长度是否为0 

Java代码   收藏代码
  1. [root@h1 test]# cat aa.sh   
  2.   
  3.   
  4. str="china"  
  5.   
  6. str2=""  
  7.   
  8.   
  9. if [  -n $str ] ; then  
  10.   
  11. echo "str is not empty"  
  12. else  
  13. echo "str is empty"  
  14. fi  
  15.   
  16.   
  17. if [  -z $str2  ] ; then  
  18. echo "str2 is empty"  
  19. else  
  20. echo "str2 is not empty"  
  21. fi  
  22.   
  23.   
  24.   
  25. time=`date`  
  26.   
  27. echo "当前时间: $time"  
  28.   
  29.   
  30.   
  31. [root@h1 test]# sh aa.sh   
  32. str is not empty  
  33. str2 is empty  
  34. 当前时间: 2014年 08月 09日 星期六 04:58:43 CST  
  35. [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旧 

Java代码   收藏代码
  1. [root@h1 test]# cat bb.sh   
  2.   
  3.   
  4.   
  5. #测试是否为目录  
  6. if [  -d $HOME  ] ; then  
  7. echo "it is a dir"  
  8. cd $HOME  
  9. ls -la  
  10. else   
  11. echo "it is not a dir"  
  12.   
  13. fi  
  14.   
  15. echo "=========================="  
  16.   
  17. #测试是否为文件  
  18.   
  19. if [ -f /etc/profile   ] ; then  
  20.   
  21. echo "it is a file"  
  22. else  
  23. echo "it is not a file"  
  24.   
  25. fi  
  26.   
  27.   
  28.   
  29.   
  30.   
  31.   
  32.   
  33.   
  34.   
  35.   
  36. [root@h1 test]# sh bb.sh   
  37. it is a dir  
  38. 总用量 329652  
  39. dr-xr-x---. 13 root   root      4096 8月   9 05:07 .  
  40. dr-xr-xr-x. 22 root   root      4096 8月   8 19:34 ..  
  41. -rw-r--r--   1 root   root 143775368 7月  28 19:30 abc1.txt  
  42. -rw-------.  1 root   root      1087 6月  13 19:06 anaconda-ks.cfg  
  43. -rw-r--r--   1 root   root        65 8月   8 04:11 a.sh  
  44. -rw-------.  1 root   root     10267 8月   9 04:22 .bash_history  
  45. -rw-r--r--.  1 root   root        18 5月  20 2009 .bash_logout  
  46. -rw-r--r--.  1 root   root       176 5月  20 2009 .bash_profile  
  47. -rw-r--r--.  1 root   root       119 6月  16 21:12 .bashrc  
  48. -rw-r--r--   1 root   root        75 8月   8 05:07 bc.sh  
  49. -rw-r--r--   1 root   root        90 8月   8 04:14 b.sh  
  50. -rw-r--r--   1 root   root       141 8月   8 05:12 cc.sh  
  51. -rw-r--r--   1 root   root        52 7月  31 21:29 count2.txt  
  52. -rw-r--r--   1 root   root        52 7月  31 19:46 count.txt  
  53. -rw-r--r--   1 root   root       127 8月   8 04:20 c.sh  
  54. -rw-r--r--.  1 root   root       100 9月  23 2004 .cshrc  
  55. -rw-r--r--.  1 root   root  96183833 6月   9 17:27 hadoop-2.2.0.tar.gz  
  56. -rw-r--r--   1 root   root         1 7月  31 21:25 hh.txt  
  57. drwxr-xr-x   3 root   root      4096 7月  29 04:47 hivesrc  
  58. -rw-r--r--.  1 root   root      2111 6月  16 13:10 initserver.sh  
  59. -rw-r--r--.  1 root   root      7995 6月  13 19:06 install.log  
  60. -rw-r--r--.  1 root   root      3384 6月  13 19:06 install.log.syslog  
  61. drwxr-xr-x   2 root   root      4096 7月  31 21:19 intest  
  62. lrwxrwxrwx   1 root   root        12 7月  31 21:45 jdk -> jdk1.7.0_25/  
  63. drwxr-xr-x.  8 search  143      4096 6月   6 2013 jdk1.7.0_25  
  64. -rwx------.  1 root   root  96316511 11月 20 2013 jdk-7u25-linux-x64.gz  
  65. drwxr-xr-x   3 root   root      4096 7月  31 21:33 li  
  66. drwxr-xr-x   3 root   root      4096 7月   9 04:08 lo  
  67. -rw-r--r--   1 root   root        25 8月   8 04:20 log.140808  
  68. drwxr-xr-x   3 root   root      4096 7月   9 04:08 login  
  69. drwxr-xr-x   3 root   root      4096 7月  29 04:11 .m2  
  70. -rw-------   1 root   root       727 7月  29 01:44 .mysql_history  
  71. -rw-r--r--   1 root   root      1048 6月  19 03:31 setlimit.sh  
  72. drwx------.  2 root   root      4096 6月  16 21:00 .ssh  
  73. -rw-r--r--.  1 root   root       129 12月  4 2004 .tcshrc  
  74. drwxr-xr-x   2 root   root      4096 8月   9 05:07 test  
  75. drwxr-sr-x   2 root   abc       4096 8月   6 01:53 testidr  
  76. -rwxr--r--   1 root   root        46 8月   8 03:43 test.sh  
  77. drwxr-xr-x   3 root   root      4096 6月  20 02:51 tsethadoop  
  78. -rw-r--r--   1 root   root        87 8月   8 05:29 t.sh  
  79. -rw-------   1 root   root      5191 8月   9 05:07 .viminfo  
  80. -rw-r--r--   1 root   root   1110849 4月   7 17:51 歌曲.mp3  
  81. ==========================  
  82. it is a file  
  83. [root@h1 test]#   



比较新旧文件的命令: 
Java代码   收藏代码
  1. [root@h1 test]# ll  
  2. 总用量 36  
  3. -rw-r--r-- 1 root root 228 8月   9 04:58 aa.sh  
  4. -rw-r--r-- 1 root root  23 8月   5 01:44 a.sh  
  5. -rw-r--r-- 1 root root 275 8月   9 05:07 bb.sh  
  6. -rw-r--r-- 1 root root  54 8月   9 04:19 b.sh  
  7. -rw-r--r-- 1 root root  90 8月   9 05:11 cc.sh  
  8. -rw-r--r-- 1 root root 145 8月   9 04:25 c.sh  
  9. -rw-r--r-- 1 root root 128 8月   9 04:37 d.sh  
  10. -rw-r--r-- 1 root root 236 8月   9 04:43 e.sh  
  11. -rw-r--r-- 1 root root  38 8月   9 04:15 test.sh  
  12. [root@h1 test]# cat cc.sh   
  13.   
  14.   
  15. if [  ./a.sh -nt ./b.sh    ] ; then  
  16. echo "a.sh is new "  
  17. else  
  18.   
  19. echo "b.sh is new"  
  20.   
  21. fi;  
  22.   
  23.   
  24. [root@h1 test]# sh cc.sh   
  25. b.sh is new  
  26. [root@h1 test]#   


多个条件组合测试: 
&&两个条件同时满足 
||满足其中一个即可 

Java代码   收藏代码
  1. [root@h1 test]# cat dd.sh   
  2.   
  3.   
  4.   
  5. if [ 3 \> 2 ] && [ 4 \> 5 ] ; then  
  6. echo "3>2 and 4>5"  
  7. else  
  8. echo "condition is not fit"  
  9.   
  10. fi  
  11.   
  12.   
  13. echo "=============================================="  
  14.   
  15.   
  16. if [ 3 \> 2 ] && [ 9 \> 5 ] ; then  
  17.   
  18. echo "3>2 and 9>5"  
  19. else  
  20. echo "condition is not fit"  
  21.   
  22. fi  
  23.   
  24.   
  25. [root@h1 test]# sh dd.sh   
  26. condition is not fit  
  27. ==============================================  
  28. 3>2 and 9>5  
  29. [root@h1 test]#   



上面的表达式比较繁琐,shell里面特意提供了针对数学运算的((exprssion))双圆括号的命令和针对字符串的双方括号的命令: 
Java代码   收藏代码
  1. [root@h1 test]# cat z.sh   
  2. if ((   4 > 6    )) ; then  
  3. echo "4 > 6"  
  4. else  
  5.   
  6. echo " 4 > 6"  
  7.   
  8. fi  
  9.   
  10.   
  11. [root@h1 test]# sh z.sh   
  12.  4 > 6  
  13. [root@h1 test]#   




双方括号提供了,更高级的字符串操作命令,利用了正则的用法: 

Java代码   收藏代码
  1. [root@h1 test]# cat h.sh   
  2.   
  3.   
  4.   
  5. if [[   "sanxian" == s*  ]] ; then  
  6.   
  7. echo "begin with s"  
  8. else  
  9.   
  10. echo "begin is not s"  
  11.   
  12. fi  
  13. [root@h1 test]# sh h.sh   
  14. begin with s  
  15. [root@h1 test]#   


注意在==号右边的正则式不能用双引号括起来 


最后再来看下多重if-else的替代命令case命令的用法: 
语法格式: 
case var in 
  pattern1 | pattern2 ) commands1:: 
  pattern3) commands2:: 
  *) default commands:: 
esac 
例子如下: 
Java代码   收藏代码
  1. [root@h1 test]# cat case.sh   
  2.   
  3.   
  4.   
  5. case "1" in  
  6.    
  7.  "1" | "11")  echo "execute this 1" ;;  
  8.  "2")   echo "execute this 2" ;;  
  9.  *)  echo "this is all default"  ;;  
  10.   
  11. esac  
  12.    
  13. [root@h1 test]# sh case.sh   
  14. execute this 1  
  15. [root@h1 test]#   



注意最后的结束符,是由两个挨着的分号构成的,通过上面的例子,我们就会发现shell里面的case语句非常强大和灵活,尽可能的使用case语句,来使我们的流程更加清晰。 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值