7月12日任务

20.5 shell脚本中的逻辑判断
20.6 文件目录属性判断
20.7 if特殊用法
20.8/20.9 case判断

20.5 shell脚本中的逻辑判断

逻辑判断在shell中随处可见,如果没有逻辑判断,就相当于单纯敲命令了,没有任何的意义,所以可见逻辑判断在shell中的重要性。

下面介绍下shell常用的3种格式,if和fi是对应的,第一种是最常用的,一般第三种使用很少。

格式1-->if 条件 ; then 语句; fi

  1. [root@jimmylinux- ~]# a=
  2. [root@jimmylinux- ~]# if [ $a -gt ] 条件语句之间要有空格分开,方括号2边也需要空格。
  3. > then
  4. > echo ok
  5. > fi
  6. ok
  7.  
  8. 以上是分开执行的效果
  9.  
  10. [root@jimmylinux- ~]# if [ $a -gt ]; then echo ok; fi
  11. ok
  12.  
  13. 这种是直接以一条命令方式执行的效果

编写shell脚本方式

  1. [root@jimmylinux- shell]# vi if1.sh
  2.  
  3. #!/bin/bash
  4. a=
  5. if [ $a -gt ]
  6. then
  7. echo ok
  8. fi

[root@jimmylinux-001 shell]# sh if1.sh  执行脚本
  ok  输出脚本结果

格式2-->if 条件; then 语句; else 语句; fi

  1. [root@jimmylinux- shell]# vi if2.sh
  2.  
  3. #!/bin/bash
  4. a=
  5. if [ $a -gt ]
  6. then
  7. echo ok
  8. else
  9. echo nook
  10. fi
  11.  
  12. [root@jimmylinux-001 shell]# sh if2.sh
  13.  
  14. nook

[root@jimmylinux-001 shell]# sh -x if2.sh
  + a=1
  + '[' 1 -gt 3 ']'
  + echo nook
  nook

格式3-->if …; then … ;elif …; then …; else …; fi

  1. [root@jimmylinux- shell]# vi if3.sh
  2.  
  3. #!/bin/bash
  4. a=
  5. if [ $a -gt ]
  6. then
  7. echo ">1"
  8. elif [ $a -gt ]
  9. then
  10. echo "<6 && >1"
  11. else
  12. echo nook
  13. fi
  14.  
  15. [root@jimmylinux- shell]# sh if3.sh
  16. nook
  17.  
  18. [root@jimmylinux- shell]# sh -x if3.sh
  19. + a=
  20. + '[' -gt ']'
  21. + '[' -gt ']'
  22. + echo nook
  23. nook

逻辑判断表达式

  1. if [ $a -gt $b ]; if [ $a -lt ]; if [ $b -eq ]等等
  2.  
  3. -gt 表示>
  4. -lt 表示<
  5. -ge 表示>=
  6. -le 表示<=
  7. -eq 表示=
  8. -ne 表示≠
  1. 也可以使用 && || 结合多个条件
  2.  
  3. if [ $a gt ] && [ $a -lt ]; then
  4. if [ $b gt ] || [ $b -lt ]; then
  5.  
  6. && 表示并且
  7. || 表示或者

20.6 文件目录属性判断

在shell中经常会和文件和目录打交道,例如判断文件或目录大小、判断是否为空等属性。

首先创建一个测试脚本,根据不同的条件进行判断。

1、[ -f file ]判断是否是普通文件,并且存在。

  1. [root@jimmylinux- shell]# vi file1.sh
  2.  
  3. #!/bin/bash
  4. f="/tmp/test666" 定义一个f变量值
  5. if [ -f $f ] 判断f这个值是否存在
  6. then
  7. echo $f exist 存在则退出
  8. else
  9. touch $f 反之touch创建f变量值
  1. fi
  1. [root@jimmylinux- shell]# sh -x file1.sh 如果文件不存在自动创建
  2. + f=/tmp/test666
  3. + '[' -f /tmp/test666 ']'
  4. + touch /tmp/test666
  5.  
  6. [root@jimmylinux- shell]# sh -x file1.sh 如果文件存在则退出
  7. + f=/tmp/test666
  8. + '[' -f /tmp/test666 ']'
  9. + echo /tmp/test666 exist
  10. /tmp/test666 exist

2、[ -d file ] 判断是否是目录,并且存在。

  1. [root@jimmylinux- shell]# vi file2.sh
  2.  
  3. #!/bin/bash
  4. f="/tmp/test666"
  5. if [ -d $f ]
  6. then
  7. echo $f exist
  8. else
  9. touch $f
  10. fi
  11.  
  12. [root@jimmylinux- shell]# sh -x file2.sh
  13. + f=/tmp/test666
  14. + '[' -d /tmp/test666 ']'
  15. + touch /tmp/test666

3、[ -e file ] 判断文件或目录是否存在

  1. [root@jimmylinux- shell]# vi file2.sh
  2.  
  3. #!/bin/bash
  4. f="/tmp/test666"
  5. if [ -e $f ]
  6. then
  7. echo $f exist
  8. else
  9. touch $f
  10. fi
  11.  
  12. [root@jimmylinux- shell]# sh -x file2.sh
  13. + f=/tmp/test666
  14. + '[' -e /tmp/test666 ']'
  15. + echo /tmp/test666 exist
  16. /tmp/test666 exist

4、[ -r file ] 判断文件是否可读

  1. [root@jimmylinux- shell]# vi file2.sh
  2.  
  3. #!/bin/bash
  4. f="/tmp/test666"
  5. if [ -r $f ]
  6. then
  7. echo $f readable
  8. fi
  9.  
  10. [root@jimmylinux- shell]# sh -x file2.sh
  11. + f=/tmp/test666
  12. + '[' -r /tmp/test666 ']'
  13. + echo /tmp/test666 readable
  14. /tmp/test666 readable
  15.  
  16. [root@jimmylinux- shell]# sh file2.sh
  17. /tmp/test666 readable

5、[ -w file ] 判断文件是否可写

  1. [root@jimmylinux- shell]# vi file2.sh
  2.  
  3. #!/bin/bash
  4. f="/tmp/test666"
  5. if [ -w $f ]
  6. then
  7. echo $f writeable
  8. fi
  9.  
  10. [root@jimmylinux- shell]# sh file2.sh
  11. /tmp/test666 writeable

6、[ -x file ] 判断文件是否可执行

  1. [root@jimmylinux- shell]# vi file2.sh
  2.  
  3. #!/bin/bash
  4. f="/tmp/test666"
  5. if [ -x $f ]
  6. then
  7. echo $f exeable
  8. fi
  9.  
  10. [root@jimmylinux- shell]# sh file2.sh 因为不可以执行,所以没有任何输出内容。
  11. [root@jimmylinux- shell]# cat !$ 因为脚本文件并没有定义else内容
  12. cat file2.sh
  13. #!/bin/bash
  14. f="/tmp/test666"
  15. if [ -x $f ]
  16. then
  17. echo $f exeable
  18. fi

例子:

20.7 if特殊用法

下面再介绍几个if的特殊用法

1、if [ -z "$n" ]  表示当变量n的值是否为空,具体操作示例如下:

  1. [root@jimmylinux- shell]# vi if4.sh
  2.  
  3. #!/bin/bash
  4. n=`wc -l /tmp/lakala`
  5. if [ $n -gt ]
  6. then
  7. echo abcdefg
  8. fi
  9.  
  10. [root@jimmylinux- shell]# sh -x if4.sh
  11. ++ wc -l /tmp/lakala
  12. wc: /tmp/lakala: 没有那个文件或目录
  13. + n=
  14. + '[' -gt ']'
  15. if4.sh: 行:[: -gt: 期待一元表达式
  1. #!/bin/bash
  2. n=`wc -l /tmp/lakala`
  3. if [ -z "$n" ]
  4. then
  5. echo error
  6. exit
  7. elif [ $n -gt ]
  8. then
  9. echo abcdefg
  10. fi
  11.  
  12. [root@jimmylinux- shell]# sh -x if4.sh
  13. ++ wc -l /tmp/lakala
  14. wc: /tmp/lakala: 没有那个文件或目录
  15. + n=
  16. + '[' -z '' ']'
  17. + echo error
  18. error
  19. + exit
  1. #!/bin/bash
  2. if [ ! -f /tmp/lakala ]
  3. then
  4. echo "/tmp/lakala not exist."
  5. exit
  6. fi
  7. n=`wc -l /tmp/lakala`
  8. if [ -z "$n" ]
  9. then
  10. echo error
  11. exit
  12. elif [ $n -gt ]
  13. then
  14. echo abcdefg
  15. fi
  16.  
  17. [root@jimmylinux- shell]# sh if4.sh
  18. /tmp/lakala not exist.

2、if [ -n "$a" ] 表示当变量a的值不为空

  1. [root@jimmylinux- shell]# ls
  2. .sh file1.sh file2.sh if1.sh if2.sh if3.sh if4.sh
  3. [root@jimmylinux- shell]# if [ -n .sh ]; then echo ok; fi
  1. ok [root@jimmylinux- shell]# echo $b [root@jimmylinux- shell]# if [ -n "$b" ]; then echo $b; else echo "b is null"; fi
  1. b is null
  1. 文件不需要加双引号引起来
  1. 变量需要使用双引号引起来
  1. if grep -q '' .txt;then...;fi -q为不显示过滤行:如果1.txt中含有123字符串的行,则执行then语句
  2.  
  3. if [ ! -e file ];then....;fi 文件不存在,则执行then语句
  4.  
  5. [ ] 中不能使用<,>,==,!=,>=,<=这样的符号

20.8/20.9 case判断

编写一个示例脚本

想实现脚本与用户的交互,你问用户做什么,用户输入什么,赋予什么值,输出的结果就是赋予的值。

  1. [root@jimmylinux- shell]# vi case.sh 编写示例脚本
  2.  
  3. 添加以下内容
  4.  
  5. #!/bin/bash
  6. read -p "Please input a number: " n
  7. if [ -z "$n" ]
  8. then
  9. echo "Please input a number."
  10. exit
  11. fi
  12.  
  13. n1=`echo $n|sed 's/[0-9]//g'`
  14. if [ -n "$n1" ]
  15. then
  16. echo "Please input a number."
  17. exit
  18. fi
  19.  
  20. if [ $n -lt ] && [ $n -ge ]
  21. then
  22. tag=
  23. elif [ $n -ge ] && [ $n -lt ]
  24. then
  25. tag=
  26. elif [ $n -ge ] && [ $n -lt ]
  27. then
  28. tag=
  29. elif [ $n -ge ] && [ $n -le ]
  30. then
  31. tag=
  32. else
  33. tag=
  34. fi
  35.  
  36. case $tag in
  37. )
  38. echo "not ok"
  39. ;;
  40. )
  41. echo "ok"
  42. ;;
  43. )
  44. echo "ook"
  45. ;;
  46. )
  47. echo "best ok"
  48. ;;
  49. *)
  50. echo "The number range is 0-100."
  51. ;;
  52. esac

shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断的更多相关文章

  1. shell脚本中的逻辑判断 文件目录属性判断 if特殊用法 case判断

    case判断 • 格式 case  变量名 in                       value1)                           command            ...

  2. Shell脚本中的逻辑判断、文件目录属性判断、if的特殊用法、case判断

    1.Shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi格式2:if 条件; then 语句; else 语句; fi格式3:if …; then … ;elif …; then ...

  3. Linux centosVMware shell脚本中的逻辑判断、文件目录属性判断、if特殊用法、case判断

    一.shell脚本中的逻辑判断 格式1:if 条件 ; then 语句; fi 格式2:if 条件; then 语句; else 语句; fi 格式3:if …; then … ;elif …; th ...

  4. Shell脚本中让进程休眠的方法(sleep用法)

    有时候写Shell的脚本,用于顺序执行一系列的程序. 有些程序在停止之后并没能立即退出,就例如有一个 tomcat 挂了,就算是用 kill -9 命令也还没瞬间就结束掉. 这么如果 shell 还没 ...

  5. shell脚本中 /dev/null 的用途

    /dev/null 是一个特殊的设备文件,它丢弃一切写入其中的数据 可以将它 视为一个黑洞, 它等效于只写文件, 写入其中的所有内容都会消失, 尝试从中读取或输出不会有任何结果,同样,/dev/nul ...

  6. Shell脚本中判断输入参数个数的方法投稿:junjie 字体:[增加 减小] 类型:转载

    Shell脚本中判断输入参数个数的方法 投稿:junjie 字体:[增加 减小] 类型:转载   这篇文章主要介绍了Shell脚本中判断输入参数个数的方法,使用内置变量$#即可实现判断输入了多少个参数 ...

  7. shell脚本中判断上一个命令是否执行成功

    shell脚本中判断上一个命令是否执行成功 shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 示例 ...

  8. centos shell脚本编程2 if 判断 case判断 shell脚本中的循环 for while shell中的函数 break continue test 命令 第三十六节课

    centos  shell脚本编程2 if 判断  case判断   shell脚本中的循环  for   while   shell中的函数  break  continue  test 命令   ...

  9. [shell]上一个命令执行完成,才执行下一个操作 | shell脚本中判断上一个命令是否执行成功

    shell脚本中判断上一个命令是否执行成功  shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 场 ...

随机推荐

  1. Ajax自我总结

    一念起.万水千山皆有情. 一念灭.沧海桑田已无心.     ------ 随记 本文主要针对ajax原理介绍,很少涉及实例,主要用于对知识的梳理总结,方便以后学习和查询... Ajax 一.Ajax是 ...

  2. 复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的 行首的空白字符及在vim中设置tab缩进为4个字符

    1.复制/etc/profile至/tmp/目录,用查找替换命令删除/tmp/profile文件中的 行首的空白字符 在命令模式下,使用正则表达式匹配 行首有空白字符行的模式:^[[:space:]] ...

  3. 关于Set和Map数据结构的一点学习

    关于js的Set和Map结构的学习和记录 对阮一峰老师的ES6入门和网上有关资料的的一点学习和记录 1.Set数据结构 Set构造函数的参数是一个可遍历( iterator)对象 Set中的成员值是唯 ...

  4. Html5在网页中引入视频音频的方法

    1.<video> 标签定义视频,比如电影片段或其他视频流. 一段简单的 HTML5 视频: <video src="movie.ogg" controls=&q ...

  5. HTTPS加密流程理解

    HTTPS加密流程 由于HTTP的内容在网络上实际是明文传输,并且也没有身份验证之类的安全措施,所以容易遭到挟持与攻击 HTTPS是通过SSL(安全套接层)和TLS(安全传输协议)的组合使用,加密TC ...

  6. 点击a标签的时候出现虚影

    在a标签中添加 outline:none;就可以去除了

  7. 使用 Zephir 轻松构建 PHP 扩展

    简介: 通过 PHP 扩展, 我们可以在 php 代码中使用一些特定的方法(大部分的 php 扩展都是用 C 写的). 比如,在 PHP 中需要与 SQLite3 交互,我们可以自己写方法与之进行连接 ...

  8. C#:转义字符 \n 和 \r 的区别

    1.\n,换行符,作用是换行符之后的字符换到下一行: 例如:1234/n567    得出的结果是1234 567 2.而\r,回车符,作用是回车符之后的字符会回到当前行的最前面,把回车符之前的字符覆 ...

  9. nyoj 55-懒省事的小明(priority_queue)

    55-懒省事的小明 内存限制:64MB 时间限制:3000ms Special Judge: No accepted:8 submit:62 题目描述:       小明很想吃果子,正好果园果子熟了. ...

  10. 理解MySQL数据库事务-隔离性

    Transaction事务是指一个逻辑单元,执行一系列操作的SQL语句. 事务中一组的SQL语句,要么全部执行,要么全部回退.在Oracle数据库中有个名字,叫做transaction ID 在关系型 ...