判断、分支、表达式(if test expression; then ..; else ...; fi;)

1、语法:

 if  commands;
      then     commands;
 [else if commands;
      then commands;
 ]
[ else commands;]
 fi

2、if 语句真正做的是:

 计算其后的命令即 commands的退出状态是成功还是失败,并据此判断下一步该执行哪些commands
      >if     true;then     echo "it's true"
           it's true

3、test 命令:

1)、 if 后面的commands 通常是test命令,test命令执行各种各样的检查与比较。它有两种等价形式,如下:

 test expression     等价于     [ expression ]

 因此:

 if [ $x = 5 ]     等价于     if     test     $x = 5

 但中括号的形式比较流行。

 2)test命令的返回结果:

 test 后的expression是一个表达式,其执行结果是true 或者false 。
 当表达式的执行结果是true时,test命令的退出状态是零。
 当表达式的执行结果是false时,test命令的退出状态是1。

4、[[]] 、(())

  1)[[]]是test命令的加强版
      [[]]增加了对一个重要的新的字符串表达式的支持:string1 =~ regex,这个expression是测试字符串是否符合所给正则。
      另一个功能是,支持==操作符用作类型匹配。

          [hadoop@hadoop03 bin]$ x=$(ls)
           [hadoop@hadoop03 bin]$ if [[ $x == iftest.* ]] ;then echo "file matches pattern iftest.* ";fi;
           file matches pattern iftest.*


 2)(())也是test命令的加强版,它 是为整数设计的,支持一套完整的算术运算:

      [hadoop@hadoop03 bin]$ x=5
      [hadoop@hadoop03 bin]$ if (( $x%2 == 0 )) ;then echo "ou shu";fi;
      [hadoop@hadoop03 bin]$ if (( $x%2 != 0 )) ;then echo "ou shu";fi;
      ou shu

5、&& 和 || 和 !
1)当两者用在expression中时,是将表达式进行结合: [ expression1 && expression2 ];

 2)当用来结合expression时,必须是用在如下形式的测试中:[],或者[[]],或者(())中,
      如果是用在 test 命令形式的测试中时,用一下运算符取代
      &&     ||     !
      -a     -o       !

释意:and or not

 2)两者也可以用来连接 多个command: command1 && command2;
      此时遵循一个短路规则,当command1退出状态为零时,不会再执行command2

4、文件表达式

表达式如果为真
file1 -ef file2file1 和 file2 拥有相同的索引号(通过硬链接两个文件名指向相同的文件)。
file1 -nt file2file1新于 file2。
file1 -ot file2file1早于 file2。
-b filefile 存在并且是一个块(设备)文件。
-c filefile 存在并且是一个字符(设备)文件。
-d filefile 存在并且是一个目录。
-e filefile 存在。
-f filefile 存在并且是一个普通文件。
-g filefile 存在并且设置了组 ID。
-G filefile 存在并且由有效组 ID 拥有。
-k filefile 存在并且设置了它的“sticky bit”。
-L filefile 存在并且是一个符号链接。
-O filefile 存在并且由有效用户 ID 拥有。
-p filefile 存在并且是一个命名管道。
-r filefile 存在并且可读(有效用户有可读权限)。
-s filefile 存在且其长度大于零。
-S filefile 存在且是一个网络 socket。
-t fdfd 是一个定向到终端/从终端定向的文件描述符 。 这可以被用来决定是否重定向了标准输入/输出错误。
-u filefile 存在并且设置了 setuid 位。
-w filefile 存在并且可写(有效用户拥有可写权限)。
-x filefile 存在并且可执行(有效用户有执行/搜索权限)。

字符串表达式

表达式如果为真
stringstring 不为 null。
-n string字符串 string 的长度大于零。
-z string字符串 string 的长度为零。
string1 = string2 string1 == string2string1 和 string2 相同. 单或双等号都可以,不过双等号更受欢迎。
string1 != string2string1 和 string2 不相同。
string1 > string2sting1 排列在 string2 之后。
string1 < string2string1 排列在 string2 之前。

警告:这个 > 和 <表达式操作符必须用引号引起来(或者是用反斜杠转义), 当与 test 一块使用的时候。如果不这样,它们会被 shell 解释为重定向操作符,造成潜在地破坏结果。 同时也要注意虽然 bash 文档声明排序遵从当前语系的排列规则,但并不这样。将来的 bash 版本,包含 4.0, 使用 ASCII(POSIX)排序规则。

整型表达式

表达式如果为真…
integer1 -eq integer2integer1 等于 integer2.
integer1 -ne integer2integer1 不等于 integer2.
integer1 -le integer2integer1 小于或等于 integer2.
integer1 -lt integer2integer1 小于 integer2.
integer1 -ge integer2integer1 大于或等于 integer2.
integer1 -gt integer2integer1 大于 integer2.

这里是一个演示以上表达式用法的脚本:

#!/bin/bash# test-integer: evaluate the value of an integer.
INT=-5
if [ -z "$INT" ]; then
    echo "INT is empty." >&2
    exit 1
fiif [ $INT -eq 0 ]; then
    echo "INT is zero."else
    if [ $INT -lt 0 ]; then
        echo "INT is negative."
    else
        echo "INT is positive."
    fi
    if [ $((INT % 2)) -eq 0 ]; then
        echo "INT is even."
    else
        echo "INT is odd."
    fifi

这个脚本中有趣的地方是怎样来确定一个整数是偶数还是奇数。通过用模数2对数字执行求模操作, 就是用数字来除以2,并返回余数,从而知道数字是偶数还是奇数。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值