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 file2 | file1 和 file2 拥有相同的索引号(通过硬链接两个文件名指向相同的文件)。 |
file1 -nt file2 | file1新于 file2。 |
file1 -ot file2 | file1早于 file2。 |
-b file | file 存在并且是一个块(设备)文件。 |
-c file | file 存在并且是一个字符(设备)文件。 |
-d file | file 存在并且是一个目录。 |
-e file | file 存在。 |
-f file | file 存在并且是一个普通文件。 |
-g file | file 存在并且设置了组 ID。 |
-G file | file 存在并且由有效组 ID 拥有。 |
-k file | file 存在并且设置了它的“sticky bit”。 |
-L file | file 存在并且是一个符号链接。 |
-O file | file 存在并且由有效用户 ID 拥有。 |
-p file | file 存在并且是一个命名管道。 |
-r file | file 存在并且可读(有效用户有可读权限)。 |
-s file | file 存在且其长度大于零。 |
-S file | file 存在且是一个网络 socket。 |
-t fd | fd 是一个定向到终端/从终端定向的文件描述符 。 这可以被用来决定是否重定向了标准输入/输出错误。 |
-u file | file 存在并且设置了 setuid 位。 |
-w file | file 存在并且可写(有效用户拥有可写权限)。 |
-x file | file 存在并且可执行(有效用户有执行/搜索权限)。 |
字符串表达式
表达式 | 如果为真 |
---|---|
string | string 不为 null。 |
-n string | 字符串 string 的长度大于零。 |
-z string | 字符串 string 的长度为零。 |
string1 = string2 string1 == string2 | string1 和 string2 相同. 单或双等号都可以,不过双等号更受欢迎。 |
string1 != string2 | string1 和 string2 不相同。 |
string1 > string2 | sting1 排列在 string2 之后。 |
string1 < string2 | string1 排列在 string2 之前。 |
警告:这个 > 和 <表达式操作符必须用引号引起来(或者是用反斜杠转义), 当与 test 一块使用的时候。如果不这样,它们会被 shell 解释为重定向操作符,造成潜在地破坏结果。 同时也要注意虽然 bash 文档声明排序遵从当前语系的排列规则,但并不这样。将来的 bash 版本,包含 4.0, 使用 ASCII(POSIX)排序规则。
整型表达式
表达式 | 如果为真… |
---|---|
integer1 -eq integer2 | integer1 等于 integer2. |
integer1 -ne integer2 | integer1 不等于 integer2. |
integer1 -le integer2 | integer1 小于或等于 integer2. |
integer1 -lt integer2 | integer1 小于 integer2. |
integer1 -ge integer2 | integer1 大于或等于 integer2. |
integer1 -gt integer2 | integer1 大于 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,并返回余数,从而知道数字是偶数还是奇数。