--------------------------------------------------------
--------------------------------------------------------
条件测试
命令成功或失败,表达式为真或假
[root@p ~]# grep pan1 /etc/passwd
pan1:x:501:502::/home/pan1:/bin/bash
[root@p ~]# echo $?
0
[root@p ~]# --------------------------------------------------------
--------------------------------------------------------
[root@p ~]# x=5;y=10
[root@p ~]# test $x -gt $y
[root@p ~]# echo $?
1
[root@p ~]# [root@panjinming ~]# x=5;y=10
You have new mail in /var/spool/mail/root
[root@panjinming ~]# [$x -gt $y]
-bash: [5: command not found
[root@panjinming ~]# [ $x -gt $y ]
[root@panjinming ~]# echo $?
1
[root@panjinming ~]#
[root@p ~]# x=5;y=10
[root@p ~]# [$x -gt $y]
-bash: [5: command not found
[root@p ~]# [ $x -gt $y ]
[root@p ~]# echo $?
1
[root@p ~]# [root@panjinming ~]# name=Tom
[root@panjinming ~]# [ $name = [Tt]?? ]
[root@panjinming ~]# echo $?
1
[root@panjinming ~]# [[ $name = [Tt]?? ]]
[root@panjinming ~]# echo $?
0
[root@panjinming ~]#
--------------------------------------------------------
|
[ -z $str] |
如果字符串 str 长度为 0,返回真 (为0 返回假) |
|
[ -n $str] |
如果字符串 str 长度为 0,返回假 (不为 0 返回真) |
|
[ $str1 = $str2 ] |
两字符串相等 |
|
[ $str1 != $str2 ] |
两字符串不等 |
[root@p ~]# name=Tom
[root@panjinmin ~]# [-z $name]
-bash: [-z: command not found
[root@p ~]# [ -z $name ]
[root@p~]# echo $?
1
[root@p ~]# [ -n $name ]
[root@p ~]# echo $?
0
[root@p ~]# name2=Andy
[root@p ~]# [ $name=$name2 ] ## 操作符两边的空格
[root@p ~]# echo $?
0
[root@p ~]# [ $name!=$name2 ] ##<span style="font-family: Arial, Helvetica, sans-serif;">操作符两边的空格</span>
[root@p ~]# echo $?
0
[root@p ~]# [ $name = $name2 ]
[root@p ~]# echo $?
1
[root@p ~]# [ $name != $name2 ]
[root@p ~]# echo $?
0
[root@panjinming ~]#
--------------------------------------------------------
整数测试
|
[ int1 -eq int2 ] |
int1 等于int2 |
|
[ int1 -ne int2 ] |
int1 不等于int2 |
|
[ int1 -gt int2 ] |
int1 大于int2 |
|
[ int1 -ge int2 ] |
int1 大于或等于int2 |
|
[ int1 -lt int2 ] |
int1 小于int2 |
|
[ int1 -le int2 ] |
int1 小于或等于int2 |
x=1; [ $x -eq 1 ];echo $? 0
x=a; [ $x -eq 1 ];echo $? -bash: [: a: integer expression expected 2
==、!=、>、>=、<、<=
x=1; let"$x==1";echo $? 0
x=1; (($x+1>= 2 ));echo $? 0
--------------------------------------------------------
|
[expr1 -a expr2 ] |
逻辑与,都为真时,结果为真 |
|
[expr1 -o expr2 ] |
逻辑或,有一个为真时,结果为真 |
|
[! expr ] |
逻辑非 |
x=1; name=Tom;
[ $x -eq 1 –a –n $name ];echo $?
0
注:不能随便添加括号
[($x-eq1)–a (–n $name)];echo $?
-bash: syntax error near unexpected token `$x'
|
[[pattern1 && pattern2 ]] |
逻辑与 |
|
[[pattern1 || pattern2 ]] |
逻辑或 |
|
[[! pattern ]] |
逻辑非 |
x=1; name=Tom;
[[ $x -eq 1 && $name = To? ]];echo $?
0
--------------------------------------------------------
[ "$name" = "" ]
[ ! "$name" ]
[ "X${name}"="X" ]
--------------------------------------------------------
文件测试
|
-f fname |
fname 存在且是普通文件时,返回真 ( 即返回 0 ) |
|
-L fname |
fname 存在且是链接文件时,返回真 |
|
-d fname |
fname 存在且是一个目录时,返回真 |
|
-e fname |
fname(文件或目录)存在时,返回真 |
|
-s fname |
fname 存在且大小大于 0 时,返回真 |
|
-r fname |
fname(文件或目录)存在且可读时,返回真 |
|
-w fname |
fname(文件或目录)存在且可写时,返回真 |
|
-x fname |
fname(文件或目录)存在且可执行时,返回真 |
--------------------------------------------------------
括号总结
|
括号形式 |
功能说明 |
|
${...} |
获取变量值 |
|
$(...) |
命令替换 |
|
$[...] |
让无类型的变量参与算术运算 |
|
$((...)) |
同上 |
|
((…)) |
算术运算 |
|
[ ... ] |
条件测试,等价于test命令 |
|
[[ ... ]] |
条件测试,支持模式匹配与通配符 |
--------------------------------------------------------
总结
本文详细介绍了Bash中的条件测试、字符串测试、整数测试、逻辑测试及文件测试,通过实例展示了如何使用Bash命令进行复杂的判断与文件操作。

被折叠的 条评论
为什么被折叠?



